Recently I came upon a Laravel project: SAM, the legacy system of our club. As an absolute newcomer in backend, I had zero idea how I should start the app.
The first thing I tried was running the
index.php file, which just printed out the HTML file in the console. Definitely not working—it's analogous to opening the
index.html file of a Vue app. You have to actually start an HTTPS service on the localhost.
Recall how a Vue app is run with yarn.
$ yarn install
$ yarn serve
Well, in Laravel, things are quite similar. The package manager used is composer, and I installed it globally following instructions here. We install the dependencies declared in
$ composer --version
Composer version 1.10.13 2020-09-09 11:46:34
$ composer install
Next, we have to declare the environment. This is the first nuance between a front end project and a back end project. Because the Vue app is run in a browser, you don't worry so much about communicating with the outside. (You are in a sandbox anyways and you access everything through URLs.) But in the back end, the app has to reach out to the server, to the data base, and to all kinds of resources. For the sake of this project, we will configure the app environment and the database connection.
Laravel comes with a sample environment file
.env.example. Duplicate it and rename it
.env. Then change the first two blocks.
The field left empty in the environment is
APP_KEY, which is used for encrypted services. Generate one using artisan.
$ php artisan key:generate
Application key [base64:yPl2zp1+ZCZ1/7TK8QEM1uQTzOetXp8pl+/bTnbLAuw=] set successfully.
We move to the database part.
DB_PASSWORD=[the root password]
I know that in production I'd better not use
root to manage the database for security concerns, but it's development anyways, so I chose the easier path. Now, login to MySQL and create a database.
$ mysql -u root -p
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 38
Server version: 8.0.23 Homebrew
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> CREATE DATABASE sam;
Query OK, 1 row affected (0.01 sec)
The tables can be automatically generated using
$ php artisan migrate
Migration table created successfully.
Lastly, start the PHP service.
$ php artisan serve