This article shows in detail how to use Laravel to register and log on to users. For more information, see
This article shows in detail how to use Laravel to register and log on to users. For more information, see
Laravel, as the most elegant PHP framework, has become a coveted PHP partner. Let's fulfill your wish today. Let's start from scratch and use Laravel to implement the most common registration and login functions for Web applications! The source code of all courses is on Github: laravel-start. Race Start!
First, let's clarify what we need for this course:
Laravel 4.2
Bootstrap 3.3
Laravel is our core concern. Bootstrap is used to quickly set some front-end CSS styles.
1. Install Laravel
After a brief description, we will proceed to the next step to install Laravel. Here we will install Laravel through Composer, open the command line terminal, and execute:
The Code is as follows:
Cd Sites
Sites is the root directory of the web application. You can replace it with your own root directory as needed, and then execute:
The Code is as follows:
Composer create-project laravel/laravel
Laravel is your application directory name. You can name it as you like. After executing the above command, wait for a while (after all, in China, the network speed is a big pitfall). After the installation, you will get this pile of directories:
We mainly operate the three directories models, controllers and views: This is the composition of MVC!
2. Install Bootstrap
Then run the following command:
The Code is as follows:
Cd laravel/public/packages
Laravel corresponds to the application directory above. If you use another name during installation, replace it with another name. Install Bootstrap in the packages directory and run the following command on the command line:
The Code is as follows:
Bower install bootstrap
This is faster. After the download, you will get the latest stable version of Bootstrap. The bower_components/bootstrap/dist/in the packages directory contains the Bootstrap css, js, and fonts style files that are frequently used during development, js and font files. You will see this:
Note: The bower tool used here is responsible for managing some front-end packages.
At this point, our preliminary work is ready. However, before proceeding to the next step, make sure that our laravel/app/storage directory has the corresponding write permission. Therefore, go back to the laravel directory. If you haven't activated the command line after installing bower, you can directly use:
The Code is as follows:
Cd ../../
Return to the laravel directory and execute:
The Code is as follows:
Chmod-R 755 app/storage
After this step is completed, we can enter the real development stage.
3. Configure the database and create a table:
Before starting the configuration, we will create a database for our laravel application. I will name it laravel-start,
Open the app/config/database. php file in the editor and fill in the corresponding database configuration items, such:
The Code is as follows:
'Default' => 'mysql ',
// Database connection
'Connections' => array (
'Mysql' => array (
'Driver '=> 'mysql ',
'Host' => '2017. 0.0.1 ',
'Database' => 'laravel-start ',
'Username' => 'root ',
'Password' => '',
'Charset' => 'utf8 ',
'Colation' => 'utf8 _ unicode_ci ',
'Prefix' => '',
),
After connecting to the database, you have to create an Users table. You can create an Users table directly in the database or use Laravel's artisan to create the table. Here we use Laravel's artisan to create the table, learn a little about Laravel migrate. Execute the following statement:
Php artisan migrate: make create-users-table
The preceding command creates a migrate file (the file is located in the app/database/migrations directory). The file name is create-users-table, then we can create the Users table by editing the generated migrate file.
The Code is as follows:
Public function up (){
Schema: create ('users', function ($ table ){
$ Table-> increments ('id ');
$ Table-> string ('username', 20 );
$ Table-> string ('email ', 100)-> unique ();
$ Table-> string ('Password', 64 );
$ Table-> string ('Remember _ token', 62)-> default ('default ');
$ Table-> timestamps ();
});
}
The above method uses laravel's Schema Builder class. The above Code uses the up () method to create a users table, which has five fields: id auto-increment, username is within 20 characters, email is within 100 characters and is unique. password is within 64 characters. remember_token is used for convenience during logon. Laravel automatically fills in the token value, however, you must set a default value timestamp at the beginning. Note that you should add the following code in down () to avoid deleting the Users table one day.
The Code is as follows:
Public function down ()
{
Schema: drop ('users ');
}
After completing the preceding steps, execute the following magic command:
The Code is as follows:
Php artisan migrate
There is a picture with the truth:
Finally, we have finished our prelude. we can officially come to Lu Laravel.
4. Start the service.
Run the following command directly in the laravel directory:
The Code is as follows:
Php artisan serve
Open your browser, enter localhost: 8000, press enter, Bingo!
OK. Give yourself 30 seconds of applause first, if you have taken this step smoothly. Congratulations, you have already entered the gate of Laravel. Let's take a look at it one by one .....
5. Create a public view
Now, let's get started. First, create a layouts folder under the app/views/folder, and then create a PHP file under the folder named main. blade. php:
The Code is as follows: