0. Default condition
This article assumes that you already have a fully configured PHP+MySQL operating environment and understand the basic knowledge of PHP website operation. By following this tutorial, you will get a basic simple blog system including login, and you will learn how to use some powerful Laravel plug-ins and composer packages (Laravel plug-ins are also composer packages).
Software version: PHP 5.4+, MySQL 5.1+
1. Installation
Many people are blocked from the first step of learning Laravel, installation. Not because the installation tutorial is complicated, but because of [well-known reasons]. Here I recommend a full Chinese image of composer: http://pkg.phpcomposer.com/. It is recommended to configure by "modifying the composer configuration file". I used this image to test when writing this tutorial, and the installation failed. If this happens to you, you can try another composer Chinese image: http://composer-proxy.com/.
After the mirror configuration is completed, switch to the directory where you want to place the website and run the command:
Then, wait for a while, a folder called learnlaravel will appear in the current directory. At this time, if you access the learnlaravel/public/ directory through the browser, Error in exception handler. will basically be displayed. This is because learnlaravel/ The app/storage directory does not have 777 permissions. After setting the permissions, you will see the page as shown below:
Congratulations~ Laravel is successfully installed!
Students who don’t want to configure the mirror can use the very famous Chaochao installation artifact in the Laravel world: https://github.com/overtrue/latest-laravel
2. Necessary plug-in installation and configuration
We use the famous Sentry plug-in to build login and other permission verification systems.
Open ./composer.json and change it to:
Then, run the command in the project root directory
Then wait for a while, it will prompt that cartalyst/sentry 2.1.4 installation is complete.
Similarly, we will install a very powerful plug-in for development, way/generators, which is its name in the composer library. Add:
to composer.jsonPlace it below “require”.
Run composer update, and then add configuration in ./app/config/app.php:
After the installation is completed, run php artisan in the command line, and you can see many new functions brought by this plug-in.
Some people may ask, why is it so slow even after using domestic mirroring? In fact, the slowest part of composer when updating is not the download, but the dependency analysis before downloading. Since Laravel relies on a lot of composer packages, and the execution speed of PHP scripts is relatively slow, it takes two or three times to wait for each update. Minutes are normal, just get used to it.
3. Database establishment 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. Here is my configuration:
prefix is the table prefix. Laravel will automatically maintain it for us, so don’t worry if you write it boldly.
At this time you need to go to the database to create this database, and then enter in the command line:
After the execution is completed, there will be 5 tables in your database, which were created by sentry itself. For details on the configuration of sentry under Laravel4, please see https://cartalyst.com/manual/sentry#laravel-4. Let me give you a rough overview:
Add the following two lines at the corresponding locations in ./app/config/app.php:
The database configuration of the permission system ends here.
Our simple blog system will have two elements, Article and Page. Next we will create articles and pages data tables and run them from the command line:
At this time, go to ./app/database/migrations, and you will see two more files. This is the database migration file. After a while, we will operate artisan to describe the two tables described by these two files. It becomes two real tables in the database. Don't worry, everything is automatic.
Below, modify in ***_create_articles_table.php:
Modify in ***_create_pages_table.php:
The following is the moment to witness the miracle, run in the command line:
At this time, the articles table and pages table in the database have been created.
4. Models
Next we will get into the most powerful part of Laravel, Eloquent ORM, the place where productivity can really be improved. To borrow a word from Cook, Goose Meiziying!
We run the following statements on the command line to create two models:
At this time, two model files appear under ./app/models/. These two classes inherit the core class Eloquent provided by Laravel.
5. Database filling
Run the following commands respectively:
At this time, two new files appear under ./app/database/seeds/, which are our database filling files. Laravel provides automatic database filling, which is very convenient.
The generator uses FakerFactory as the random data generator by default, so we need to install this composer package, the address is https://packagist.org/packages/fzaninotto/faker, and install it together with the generator in require-dev. Please complete the specific installation by yourself. You can refer to Sentry and Generator. This is the first exercise.
Next, change these two files individually:
Then, we need to add two lines to DatabaseSeeder.php so that Laravel will bring the two new seed files we added when seeding.
The next step is to actually fill the data into the database:
After the operation is completed, go to the database and see that the data has been filled in, with 10 rows each for article and page.
itboba.com/taxonomy/term/263
What is this for? ? ?