Laravel is a PHP framework that provides structure and tools for developing web applications.
In the previous module, you learned how to use Composer. Laravel leverages that ecosystem, but it is a complete framework: it provides a project structure, configuration, and tools so you don’t start from scratch.
Creating a new project
Forget about manually creating src, public, or vendor folders. Laravel does it all.
First, install the official Laravel CLI via Composer:
composer global require laravel/installerThen go to your projects folder and create the application:
laravel new my-super-appWhat is happening?
The installer will ask you about the database, the testing system, and, if you wish, a starter kit. When finished, you’ll have a my-super-app folder with Laravel and its dependencies.
Starting the server (serve)
To start the full development environment, install the frontend resources and use the script included by Laravel:
Enter the folder and start it up:
cd my-super-app
npm install && npm run build
composer run devOpen http://localhost:8000 in your browser. The command keeps the development server and the tools needed by the project running. For a quick test, you can also use php artisan serve.
What is Artisan?
Inside the Laravel folder, you will see a file called artisan. It is not a file to edit, but the framework’s command-line interface (CLI).
Think of it this way: Artisan is your robot assistant.
Instead of creating a PHP file, writing class User..., saving it, etc., you give an order to Artisan, and it writes the boilerplate code for you.
Basic Artisan commands
To use it, you must always be in the root of your project (where the artisan file is located) and type php artisan [command].
Try writing this to see everything your assistant can do:
php artisan listHere are the commands you will use 80% of the time:
| Command | What does it do? | Manual equivalent |
|---|---|---|
php artisan serve | Starts the web server. | Configuring Apache/Nginx. |
php artisan make:controller | Creates a Controller file. | Create file, write namespace, class… |
php artisan make:model | Creates a Model for the database. | Create class, connect to PDO… |
php artisan make:migration | Creates a file to modify the database. | Writing SQL CREATE TABLE manually. |
php artisan route:list | Shows all the URLs of your website. | Read your code to see which links you have. |
php artisan optimize:clear | Clears the cache if something gets stuck. | Delete temporary files manually. |
Your first “hello world” with Artisan
Let’s ask Artisan to create an empty controller for us to test that it works. Instead of creating the file manually, we write:
php artisan make:controller PruebaControllerIf you go to the app/Http/Controllers/ folder, you will see that a PruebaController.php file has appeared with the class already defined, the correct namespace, and everything ready to start working.