Laravel folder structure
This is some kind of sequel of my previous tutorial A complete beginners introduction to web development.
So Laravel eh. A term you might have seen before, or maybe you haven’t seen it before. In this tutorial I will assume you have basic experience with Apache, MySQL and PHP. I will reference to the Terminal every now and then (On Windows this is called Command Prompt).
Before we begin lets install a fresh new Laravel project. Type the following in your Terminal:
$ composer create-project laravel/laravel
If you take a look in the current folder you will see a folder “laravel” where there is a fresh installation of Laravel. Go into the new laravel folder with your Terminal and try running “php artisan serve”. This will fire a PHP server so you can view your application. You will be able to browse to your application by going to http://localhost:8000. If the installation was successful you will see the content like the screenshot above.
Laravel ships with a very powerful feature to generate placeholder code for you, this is the so called “artisan”. With the artisan you can generate and perform various actions. If we run “php artisan” in the Terminal you will get a list of available commands Laravel brings.
Folder structure
So lets take a look at the folder structure of Laravel. Laravel follows the Model View Controller (MVC) structure but at the beginning it might be hard to find where what is located. We will start with the M in MVC, Models. Models don’t have their own folder with a standard Laravel installation. If we look at the “app” folder you will see the following structure:
app/
Console/
Exceptions/
Http/
Controllers/
Middleware/
Providers/
User.php
By default, Laravel puts models in the app/ folder, but as your app grows it better to place them in a separate folder. For now it’s fine to just let your Models stay in the app/ folder. But what are Models? Models represent data from any kind of data source. In the beginning stages of you as a web developer this will mostly be a record from a MySQL database. As you can see there is already a model called User.php. This represents the data of 1 user in the database. For now there are no tables in your database yet but that’s not important for this tutorial.
The next letter in MVC is the V, which stands voor Views. View contain all the HTML to show the right data to the visitor of your website. There is a folder called “resources” in your project root. The folder has the following content:
resources/
assets/
lang/
views/
home.blade.php
welcome.blade.php
In the folder views are all views located (duhh…). Here you will find the HTML your website will display. Views are indicated with the extension “.blade.php”. As you will evolve in your Laravel skills you will understand why this is, for now I will let this rest.
Last but not least we have the Controllers in MVC. Controllers will handle what data to display in what View. It’s the relationship between a View and a Model. Controllers are located in the folder “app/Http/Controllers” There are some default controllers present when you install Laravel. These Controllers take care of the login and register functionality Laravel provides out of the box. As you have seen you can already see some content when you browse to http://localhost:8000. This is content is described in the “welcome.blade.php”. In the following example we are going to refactor the display approach of this view.
In your project root there is a folder called “routes”. In the routes folder are all URL’s defined, your application has. There are four files by default:
routes/
api.php
channels.php
console.php
web.php
For now, we only focus on the web.php file. This file defines are your URLs available through a browser. As you open this file, you see there is already one URL defined. This is the page when you browse to http://localhost:8000. Normally, I don’t display views from within this file so we are going to refactor this to a separate controller. Because Controllers show Views! Open up your terminal and run the following command:
$ php artisan make:controller HomeController
This will create a Controller located in “app/Http/Controllers”. If you open the HomeController.php file you will see an empty class with no functions or anything. Lets create a function which is responsible for showing the homepage.
/**
* Show homepage
* @return View
*/
public function index()
{
return view('welcome');
}
With the “view()” function, you can display a View from the “resources/views” folder. Heads up! You don’t need the “.blade.php” extension when you call the function. Now that we have defined our homepage controller, we still have to tell Laravel when this function needs to get called for which URL. This can be achieved by editing the “routes/web.php” file. Remove the default PHP code and insert the following code:
Route::get('/', 'HomeController@index');
This means that when a user is browsing to your homepage the function “index” will get called from the controller “HomeController”. If you now browse to http://localhost:8000 you will still see the welcome page :)
Try to add some more Views and URL’s with separate Controller methods!