Skip to main content

Running a Laravel app

· 3 min read

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.

Install dependencies

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.json.

$ composer --version
Composer version 1.10.13 2020-09-09 11:46:34
$ composer install

Configure environment

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.

.env
APP_NAME=sam
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

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.

Configure database

We move to the database part.

.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=sam
DB_USERNAME=root
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
Enter password:
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
owners.

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)

mysql> quit;
Bye

The tables can be automatically generated using

$ php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
...

Start service

Lastly, start the PHP service.

$ php artisan serve