This article does not recommend learning PHP and MVC programming. This document is not a "Step by Step" tutorial. This article requires you to make some mental efforts to solve some hidden tasks, big or small, to truly understand Laravel's operating logic.
0. Default Conditions
By default, you already have a well-configured PHP + MySQL runtime environment to understand the basics of running PHP websites. After following this tutorial, you will get a basic simple blog system that includes logon, we will also learn how to use some powerful Laravel plug-ins and composer packages (the Laravel plug-in is also a composer package ).
Software Version: PHP 5.4 +, MySQL 5.1 +
1. Install
Many people are blocked from installing Laravel in the first step. It is not because of the complexity of the installation tutorial, but because of [well-known reasons ]. Here I recommend a full-scale Chinese image of composer: http://pkg.phpcomposer.com /. We recommend that you modify the composer configuration file. I used this image for testing when writing this tutorial. The installation failed. If this happens, try another composer Chinese Image: http://composer-proxy.com /.
After the image is configured, switch to the directory where you want to place the website and run the following command:
The Code is as follows:
Composer create-project laravel/laravel learnlaravel
Then, wait a moment and a folder named learnlaravel will appear in the current directory. If you access the learnlaravel/public/directory through a browser, the Error in exception handler will be displayed ., this is because the learnlaravel/app/storage directory does not have the 777 permission. After setting the permission, you can see the page as follows:
Congratulations ~ Laravel is successfully installed!
Do not want to configure the image of the students, you can use the Laravel industry is very famous ultra installation artifacts: https://github.com/overtrue/latest-laravel
2. install and configure necessary plug-ins
We use the famous Sentry plug-in to build a logon and Other permission verification system.
Open./composer. json and change it:
The Code is as follows:
"Require ":{
"Laravel/framework": "4. 2 .*",
"Cartalyst/sentry": "2.1.4"
},
Then, run the command in the project root directory.
The Code is as follows:
Composer update
Wait a moment and it will prompt that the installation of cartalyst/sentry 2.1.4 is complete.
Similarly, we will install a very powerful plug-in for development, way/generators, which is its name in the composer library. Add the following content to composer. json:
The Code is as follows:
"Require-dev ":{
"Way/generators ":"~ 2.0"
},
Put it under "require.
Run composer update and add the configuration in./app/config/app. php:
The Code is as follows:
'Way \ Generators \ GeneratorsServiceProvider'
After the installation is complete, run php artisan in the command line to view many new functions provided by this plug-in.
Some people may ask why domestic images are still so slow? In fact, the slowest part of composer during update Is Not to download, but to download the previous dependency analysis. Because Laravel relies on a large number of composer packages, PHP scripts are executed slowly, therefore, it is normal to wait two or three minutes for each update.
3. database creation and migration
The database configuration file is located in./app/config/database. php. We need to change the "mysql" item in "connections" to the configuration we need. The following is my Configuration:
The Code is as follows:
'Mysql' => array (
'Driver '=> 'mysql ',
'Host' => 'localhost ',
'Database' => 'laravel ',
'Username' => 'root ',
'Password' => 'Password ',
'Charset' => 'utf8 ',
'Colation' => 'utf8 _ unicode_ci ',
'Prefix' => 'l4 _',
),
The prefix is the table prefix. This Laravel will be automatically maintained and you don't have to worry about writing it.
In this case, you need to go to the database to create the database, and then enter:
The Code is as follows:
Php artisan migrate -- package = cartalyst/sentry
After the execution is complete, five tables are in your database, which is created by sentry. For details about how to configure sentry in Laravel4, refer to https://cartalyst.com/manual/sentry?laravel-4,4:
Add the following two lines to the corresponding position in./app/config/app. php:
The Code is as follows:
'Cartalyst \ Sentry \ SentryServiceProvider ',
'Sentry' => 'cartalyst \ Sentry \ Facades \ Laravel \ Sentry ',
The database configuration of the permission system ends here.
In our simple blog system, there will be two elements, Article and Page. Next we will create the articles and pages data tables and run the command line:
The Code is as follows:
Php artisan migrate: make create_articles_table -- create = articles
Php artisan migrate: make create_pages_table -- create = pages
At this time, go. /app/database/migrations, two more files are displayed. This is the database migration file, after a while, we will operate artisan to convert the two tables described in the two files into the real two tables in the database. Rest assured that everything is automatic.
Modify the following in *** _ create_articles_table.php:
The Code is as follows:
Schema: create ('articles ', function (Blueprint $ table)
{
$ Table-> increments ('id ');
$ Table-> string ('title ');
$ Table-> string ('slug')-> nullable ();
$ Table-> text ('body')-> nullable ();
$ Table-> string ('image')-> nullable ();
$ Table-> integer ('user _ id ');
$ Table-> timestamps ();
});
In *** _ create_pages_table.php, modify:
The Code is as follows:
Schema: create ('pages ', function (Blueprint $ table)
{
$ Table-> increments ('id ');
$ Table-> string ('title ');
$ Table-> string ('slug')-> nullable ();
$ Table-> text ('body')-> nullable ();
$ Table-> integer ('user _ id ');
$ Table-> timestamps ();
});
The following is the time to witness the miracle and run it in the command line:
The Code is as follows:
Php artisan migrate
At this time, the articles table and pages table in the database are created.
4. Model Models
Next we will be connected to Laravel's most powerful part, Eloquent ORM, where we can really improve productivity. In the words of Cook, goose girl Ying!
Run the following statement on the command line to create two models:
The Code is as follows:
Php artisan generate: model article
Php artisan generate: model page
At this time, two model files appear under./app/models. These two classes inherit from the core class \ Eloquent provided by Laravel.
5. Database Filling
Run the following commands:
The Code is as follows:
Php artisan generate: seed page
Php artisan generate: seed article
At this time, two new files appear under./app/database/seeds/. This is our database filling file. Laravel provides automatic database filling, which is very convenient.
Generator uses Faker \ Factory as the random data generator by default, so we need to install this composer package, the address is a https://packagist.org/packages/fzaninotto/faker, installed in require-dev with generator. Complete the installation by yourself. Refer to Sentry and Generator. This is the first exercise.
Next, modify the two files respectively:
The Code is as follows:
Article: create ([
'Title' => $ faker-> sentence ($ nbWords = 6 ),
'Slug' => 'first-post ',
'Body' => $ faker-> paragraph ($ nbSentences = 5 ),
'User _ id' => 1,
]);
Page: create ([
'Title' => $ faker-> sentence ($ nbWords = 6 ),
'Slug' => 'first-page ',
'Body' => $ faker-> paragraph ($ nbSentences = 5 ),
'User _ id' => 1,
]);
Then, we need to add two lines in DatabaseSeeder. php so that Laravel will include the two newly added seed files when seed.
The Code is as follows:
$ This-> call ('articletablesecret ');
$ This-> call ('pagetableseeder ');
We need to fill the data in the database:
The Code is as follows:
Php artisan db: seed
After the operation is complete, go to the database and check that the data has been filled in. There are 10 rows in each article and page.