0. Default conditions
This article by default you already have the well-configured Php+mysql running environment, understand the PHP website operation basic Knowledge. Following this tutorial, you will get a basic simple blog system with login, and will learn how to use some powerful laravel plugins and composer packages (Laravel plugins are also composer packages).
Software version: PHP 5.4+,mysql 5.1+
1. Installation
Many people were stopped in the first step of learning Laravel, installed. This is not because of the complexity of the installation tutorials, but because of "well-known causes." Here I recommend a composer full amount Chinese mirror: http://pkg.phpcomposer.com/. It is recommended to configure the "Modify composer Profile" mode. I am writing this tutorial with this image test, installation failed, if you also have this situation, you can try another composer Chinese mirror: http://composer-proxy.com/.
After the mirroring configuration is complete, switch to the directory where you want to place the site, and run the command:
Copy the Code code as follows:
Composer Create-project Laravel/laravel Learnlaravel
Then, wait a moment, there will be a folder called Learnlaravel in the current directory, if you access the learnlaravel/public/directory through the browser, the basic will show the Error in exception handler. , this is because the Learnlaravel/app/storage directory does not have 777 permissions, set permissions to see the page such as:
Congratulations on your ~laravel installation success!
Do not want to configure the image of the classmate, you can use the Laravel world is very famous super Super get to install artifact: https://github.com/overtrue/latest-laravel
2. Necessary plug-in installation and configuration
We use the well-known sentry plugin to build a login and other authorization authentication system.
Open a./composer.json, change to:
Copy the Code code as follows:
"Require": {
"Laravel/framework": "4.2.*",
"Cartalyst/sentry": "2.1.4"
},
Then, run the command under the project root directory
Copy the Code code as follows:
Composer Update
Then wait a moment and it will prompt cartalyst/sentry 2.1.4 installation is complete.
In the same vein, we will install a very powerful plugin for development, Way/generators, which is its name in the composer library. Added in Composer.json:
Copy the Code code as follows:
"Require-dev": {
"Way/generators": "~2.0"
},
Put it under "require".
Run composer Update, and then add the configuration in./app/config/app.php:
Copy the Code code as follows:
' Way\generators\generatorsserviceprovider '
Once the installation is complete, run PHP artisan on the command line to see the many new features that this plugin brings.
Some people will ask, why use the domestic mirror or so slow? In fact composer in the update when the slowest place is not download, but download the dependency resolution, because Laravel relies on the composer package is very much, PHP script execution speed is relatively slow, so every update, such as two or three minutes is normal, habit is good.
3. Database Setup and Migration
The database configuration file is located in./app/config/database.php, and we need to change the "MySQL" Item in "Connections" to the configuration we need. Here is my configuration:
Copy the Code code as follows:
' MySQL ' = Array (
' Driver ' = ' mysql ',
' Host ' = ' localhost ',
' Database ' = ' laravel ',
' Username ' = ' root ',
' Password ' = ' password ',
' CharSet ' = ' utf8 ',
' Collation ' = ' utf8_unicode_ci ',
' Prefix ' = ' l4_ ',
),
Prefix is the table prefix, this laravel will help us automatically maintenance, bold write Don't worry.
At this point you need to go to the database to build this database and then enter it at the command line:
Copy the Code code as follows:
PHP Artisan Migrate--package=cartalyst/sentry
After the execution, you have 5 tables in your database, which sentry created yourself. Sentry configuration details under Laravel4 see https://cartalyst.com/manual/sentry#laravel-4, I'll say a little bit about:
Add the following two lines to the corresponding locations in the./app/config/app.php:
Copy the Code code as follows:
' Cartalyst\sentry\sentryserviceprovider ',
' Sentry ' = ' cartalyst\sentry\facades\laravel\sentry ',
The database configuration for the permissions system ends here.
Our simple blog system will have two elements, article and page, below we will create articles and pages data tables, command line run:
Copy the Code code as follows:
PHP Artisan migrate:make create_articles_table--create=articles
PHP Artisan migrate:make create_pages_table--create=pages
At this time, go to./app/database/migrations, will see more than two files, this is the database migration files, after a while we will operate artisan the two tables described in these two files into a database of two real tables, rest assured that everything is automatic.
Below, modify in ***_create_articles_table.php:
Copy the Code code 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 ();
});
Modify in ***_create_pages_table.php:
Copy the Code code 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 a miracle, run in the command line:
Copy the Code code as follows:
PHP Artisan Migrate
The articles and pages tables in the database are now established.
4. Model Models
Next we will touch the most powerful part of Laravel, eloquent ORM, really improve the productivity of the place, borrow Cook's words to say, goose sister English!
We run the following statement at the command line to create two model:
Copy the Code code as follows:
PHP Artisan Generate:model Article
PHP Artisan Generate:model Page
At this time, two model files appeared under./app/models/. These two classes inherit the core class \eloquent provided by Laravel.
5. Database population
Run the following command, respectively:
Copy the Code code as follows:
PHP Artisan generate:seed Page
PHP Artisan generate:seed Article
At this point, two new files appear under the./app/database/seeds/, which is our database fill 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 https://packagist.org/packages/fzaninotto/faker, Install with generator in Require-dev. Specific installation Please do it yourself, you can refer to Sentry and generator, this is the first practice.
Next, change these two files individually:
Copy the Code code 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 rows to the databaseseeder.php, so that Laravel will bring our new two seed files in seed.
Copy the Code code as follows:
$this->call (' Articletableseeder ');
$this->call (' Pagetableseeder ');
The next step is to actually populate the database with the data:
Copy the Code code as follows:
PHP Artisan Db:seed
After the operation is done to the database to see, the data has been filled in, article and page each 10 lines.