Laravel is the most elegant PHP framework. Many friends who learn PHP are coveting Laravel. Come realize your wish today. Let us start from scratch and use Laravel to implement the most common registration and login functions of web applications! All course source codes have been placed on Github: laravel-start. Race Start!
First, let’s clarify what we need for this course:
Laravel 4.2
Bootstrap 3.3
Laravel is the core part we care about, and Bootstrap is used to quickly set some front-end CSS styles.
1. Install Laravel
After a brief explanation, let’s go to the next step and install Laravel. Here we install it through Composer. Open the command line terminal and execute:
Sites is the root directory of the web application. You can change it to your own root directory as needed, and then execute:
Laravel is the name of your application directory. You can choose a name you like. After executing the above command, wait for a while (after all, Internet speed is a big problem in China). After the installation is completed, you will get this bunch of directories:
We mainly operate three directories: models, controllers and views: this is the composition of MVC!
2. Install Bootstrap
Then execute from the command line:
Laravel here corresponds to the application directory above. If you used another name during installation, please change it accordingly. Go to the packages directory to install Bootstrap and execute it directly on the command line:
This is faster, and after this is downloaded, you will get the latest stable version of Bootstrap. Bower_components/bootstrap/dist/ in the packages directory contains Bootstrap's css, js, and fonts, three style files, js, and font files that we often use during the development process. After success you will see this:
Note: The tool bower used here is responsible for managing some front-end packages.
At this point, our preliminary work is ready. But before proceeding to the next step, we must first ensure that our laravel/app/storage directory has corresponding write permissions, so return to the laravel directory. If you have not touched the command line after installing bower, you can directly pass:
Go back to the laravel directory and execute:
After this step is completed, we can enter the real development stage.
3. Configure the database and create tables:
Before starting the configuration, we are going to create a database for our laravel application, I will name it laravel-start,
Then open the app/config/database.php file in the editor and fill in the corresponding database configuration items, such as:
After connecting to the database, you have to create a Users table. You can create the Users table directly in the database, or you can use Laravel's artisan to create it. Here we use Laravel's artisan to create the table, and learn a little bit about Laravel along the way. Knowledge of migrate. Execute the following statement:
php artisan migrate:make create-users-table
The above command will create a migrate file (the file is located in the app/database/migrations directory). The name of this file is create-users-table. Then we can create the Users table by editing the migrate file we just generated.
The above method uses laravel's Schema Builder class. The above code uses the up() method to create a users table. There are 5 fields in this table: id auto-increment, username length within 20, and email length within 100 And it is unique. The password length should be within 64. Remember_token is to make it more convenient and practical when logging in. Laravel will automatically fill in the token value, but at the beginning you must set a default value, timestamp the current timestamp. One thing we need to pay attention to here is: It is best to add the following code to down() in case we need to delete the Users table one day.
After doing the above, execute the following magical command:
There are pictures and the truth:
Finally, we have finished the prelude and can officially come to Laravel.
4. Start the service and try it
Execute directly in the laravel directory:
Open the browser, enter localhost:8000, press Enter, Bingo!
OK, give yourself thirty seconds of applause first, if you have successfully reached this point. Congratulations, you have entered the door of Laravel, we will have more surprises to come...
5. Create a public view
Okay, let’s start now. First create a layouts folder under the app/views/ folder, then create a new php file under this folder, name it main.blade.php, and write in this file The following codes: