# Introduction to Laravel

> An introduction to Laravel: set up PHP and Composer, create a project, run it with php artisan serve, and learn how routes and Blade views render your pages.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2023-06-02 | Topics: [Laravel](https://flaviocopes.com/tags/laravel/) | Canonical: https://flaviocopes.com/introduction-to-laravel/

To get started with Laravel, you need to set up your PHP environment on your computer.

You can do this in various ways.

Before going on, remove any older PHP installations you might have done in the past. How exactly depends on how you installed PHP on your machine. Hopefully you haven’t any and we can go on.

On macOS, use Homebrew ([install Homebrew](https://brew.sh/) first if you haven’t already) and install both PHP and [Composer](https://flaviocopes.com/php-composer-packagist/) using 

```shell
brew install php composer
```

(might take a while)

Once installed you should be able to run the `php -v` command to get the version of PHP installed (same for `composer -v`):

![Terminal showing PHP version 8.2.8 and Composer version 2.5.8 command outputs](https://flaviocopes.com/images/introduction-to-laravel/1.webp)

Now you can go into folder on your computer that you reserve for development. I have a `dev` folder in my home directory, for example.

In there, run:

```shell
composer create-project laravel/laravel first
```

To create a new project in a folder called `first`.

Now go into that folder and run `php artisan serve`:

```shell
cd first
php artisan serve
```

![Terminal output showing Laravel development server starting and serving on http://127.0.0.1:8000](https://flaviocopes.com/images/introduction-to-laravel/2.webp)

`php artisan <some_command>`  is something you’ll use often in Laravel, as it can do a lot of useful stuff for you. For example we’ll use it to “scaffold” models without having to create files by hand.

Open your browser and you’ll see the default welcome screen of a Laravel app:

![Laravel default welcome page showing the Laravel logo and welcome message in a browser](https://flaviocopes.com/images/introduction-to-laravel/3.webp)

If you have troubles reaching this stage, the official documentation has great guides for [macOS](https://laravel.com/docs/10.x#getting-started-on-macos), [Linux](https://laravel.com/docs/10.x#getting-started-on-linux) and [Windows](https://laravel.com/docs/10.x#getting-started-on-windows).

Open the newly created project folder in VS Code.

This should be the file structure:

![VS Code file explorer showing Laravel project structure with folders like app, config, routes, resources](https://flaviocopes.com/images/introduction-to-laravel/4.webp)

While you’re here I recommend you install the extensions

- [Laravel Extra Intellisense](https://marketplace.visualstudio.com/items?itemName=amiralizadeh9480.laravel-extra-intellisense)
- [Laravel Artisan](https://marketplace.visualstudio.com/items?itemName=ryannaddy.laravel-artisan)
- [Laravel Blade Snippets](https://marketplace.visualstudio.com/items?itemName=onecentlin.laravel-blade)
- [PHP tools for VS Code](https://marketplace.visualstudio.com/items?itemName=DEVSENSE.phptools-vscode)

We have a bunch of folders and a bunch of files.

The first thing you’re going to look at is the `.env` file.

It contains a lot of configuration options, called environment variables, for your app:

![Contents of .env file showing configuration variables like APP_NAME, APP_ENV, APP_DEBUG, and database settings](https://flaviocopes.com/images/introduction-to-laravel/5.webp)

For example in this portion of the file you can see we set the app name, the debug flag, the URL, settings related to logging, to the database connection, email sending and much more.

One very useful folder is `config`. Here’s for example the `config/app.php` file: 

![Code editor showing config/app.php file with PHP configuration arrays and env() helper function calls](https://flaviocopes.com/images/introduction-to-laravel/6.webp)

Each file in the folder contain a lot of configuration options you can set, very well documented.

What’s the difference between config files and the `.env` file? Environment variables in `.env` can be changed depending on the deployment, for example locally in development you can have debug enabled, while on the production server you don’t want that. 

Some options in `config` files, like the ones you see above, make use of the `env()` Laravel helper function to get the environment variable.

While options stored directly in the `config` folder hardcoded are “one for all environments”.

Before looking at changing any of the configuration options, let’s modify what you see in the browser.

Open the `routes` folder and you’ll 4 files. Open `web.php`:

![Code editor displaying routes/web.php file with Route::get method defining the root path handler](https://flaviocopes.com/images/introduction-to-laravel/7.webp)

This is the code that displays the sample home page of the Laravel application:

![Laravel welcome page displayed in browser showing the framework logo and documentation links](https://flaviocopes.com/images/introduction-to-laravel/8.webp)

We made a request to the `/` relative URL (`http://127.0.0.1:8000`**`/`****)**, which means the “home page”. 

This URL is handled in the `routes/web.php` file, which contains the router dedicated to handling HTTP requests coming from the browser.

In this file, as shown in the screenshot, we tell Laravel to return the `welcome` view when someone visits the `/` URL using the `GET` HTTP method (the one used when you open the page in the browser):

```php
Route::get('/', function () {
    return view('welcome');
});
```

To do this we use the `view()` Laravel helper, which knows where to find the `welcome` view because Laravel uses a set of conventions. 

We have folders and files dedicated to holding specific, well-defined parts of our applications.

In this case the `welcome` view is defined in the file `resources/views/welcome.blade.php`:

![Code editor showing resources/views/welcome.blade.php file with HTML and Blade template code](https://flaviocopes.com/images/introduction-to-laravel/9.webp)

You can clear all the content of this file, and type `<h1>test</h1>` into it. Save (cmd-s or ctrl-s) and reload in the browser, the homepage content will switch to displaying this string:

![Browser showing simple test page with only the text test displayed as h1 heading](https://flaviocopes.com/images/introduction-to-laravel/10.webp)

So now you know for sure that this file is responsible for what’s shown on that URL!

Now let’s add a second page.

In `routes/web.php`, add:

```php
//...

Route::get('/test', function () {
    return view('welcome');
});
```

This will render the `welcome` view also when the `/test` route is called:

![Code editor showing routes/web.php with additional Route::get for /test path added to the file](https://flaviocopes.com/images/introduction-to-laravel/11.webp)

![Browser displaying the test page at /test URL showing the same test heading as the homepage](https://flaviocopes.com/images/introduction-to-laravel/12.webp)

You can show a different content by creating a new view in `resources/views` and using that view in the route, for example create a new view `resources/views/test.blade.php`

`resources/views/test.blade.php`

```php
<h1>new view!</h1>
```

`routes/web.php`

```php
//...

Route::get('/test', function () {
    return view('test');
});
```

Here is the result:

![Browser showing the /test page with new view! text displayed after creating separate test.blade.php file](https://flaviocopes.com/images/introduction-to-laravel/13.webp)

Notice that any URL that does not have a specific entry in `routes/web.php` renders a “404 not found” page:

![Browser showing Laravel default 404 not found error page for an undefined route](https://flaviocopes.com/images/introduction-to-laravel/14.webp)

You can customize this error page. Here’s how: create an `errors` folder in `resources/views`, and in there create a `404.blade.php` file. Add any content, like

```php
<img src="https://media.tenor.com/IHdlTRsmcS4AAAAM/404.gif" />
```

And this will be rendered for 404 errors:

![Browser displaying custom 404 error page with animated GIF showing 404 message](https://flaviocopes.com/images/introduction-to-laravel/15.webp)

You didn’t have to do anything more than creating the file, because Laravel has this set of conventions, so adding a file in the right place with the right name will do something specific.
