1. New Laravel Project, Authentication and Routes
Main points of this lesson
Install Laravel installer on your computer with this command
composer global require "laravel/installer"
Then, create new Laravel project every time with this command:
laravel new project
It will create a folder called "project" in your current folder and install Laravel there.
You will have .env file to manage all your configuration, probably most important settings there are:
- DB_DATABASE
- DB_USERNAME
- DB_PASSWORD
- APP_URL
There's also a file called .env.example, read about the differences and how to use them in this article.
To create Login and Register routes and links, use this command:
php artisan make:auth
Laravel comes with two database migrations out-of-the-box: for tables users and password_resets.
If you want your URLs to be accessed only by logged in users, use Route::group() structure and add Middleware 'auth' to it.
In routes/web.php file, you can specify Route URL and attach it to a specific method of specific Controller:
Route::get('invoices/create', 'InvoicesController@create');
Specify route name with this structure:
Route::get('invoices/create', 'InvoicesController@create')->name('invoices.create');
Then you can use it everywhere in Blade files with command route('invoices.create').
It is useful if you decide to change the URLs sometime in the future - you would have to change only routes/web.php file, and all the links in Blade files will remain working without changing.
To create a controller, use this Artisan command:
php artisan make:controller InvoicesController
Blade files parent-child structure is based on these commands:
- Create your "parent" Blade file which will be the same for all pages, will contain static header/footer and basic structure.
- In that "parent" Blade file, specify dynamic areas with command @yield, like @yield('content')
- In every "child" Blade file, specify its parent by using @extends command, like @extends('layouts.app')
- Notice: If you want to reference Blade files within subfolders of resources/views, use dot symbol in the name, like resources/views/layouts/app.blade.php is called as @extends('layouts.app')
- Also, in every "child" Blade file put the contents of dynamic areas with @section command, like @section('content') and at the end of section call @stop or @endsection
Code for download
Here's Link to Google Drive
How to use: download, unarchive, change credentials in .env file, launch php artisan migrate, and you should be good to go.
Links to read more
- Laravel installation process
- Laravel Homestead
- Environment configuration and .env files
- Authentication Quickstart and make:auth command
- Database Migrations (we will come back to this topic in much more detail later in the course)
- Laravel Routing: General
- Laravel Routing: Named Routes
- Laravel Routing: Route Groups and Middleware
- Laravel Controllers
- Laravel Blade Templates: General
- Laravel Blade Templates: Structure and Inheritance
- MVC Pattern and PHP: not directly about Laravel, but for those who want to understand/remember the code Model-View-Controller structure
4 comments