Laravel 5 Series Introductory Tutorial (i) "Laravel course for the Chinese"

Source: Internet
Author: User
Tags php website

Laravel 5 Series Introductory Tutorial (i) "Laravel course for the Chinese"Share ⋅johnlui 2 years ago . Last reply by skys215 11 months ago ⋅17543 Read

Published in my personal website: Laravel 5 Series Introductory Tutorial (i) "Laravel course for the Chinese"

The sample code for this tutorial is shown in: https://github.com/johnlui/Learn-Laravel-5

The quickest way to get stuck in any place is to look at my sample code.

Laravel 5 Chinese documents:

    1. http://laravel-china.org/docs/5.0
    2. http://www.golaravel.com/laravel/docs/5.0/
Default condition

This article by default you already have the well-configured PHP + MySQL running environment, understand the basic knowledge of PHP website operation. 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+

This article does not recommend learning from people who do not understand PHP and MVC programming at all. This article is not a "step-by-Step with Me" tutorial. This article requires a certain mind to solve some large or small hidden tasks, in order to achieve a true understanding of the purpose of Laravel running logic.

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.

After the mirroring configuration is complete, switch to the directory where you want to place the site (such as C:\\wwwroot,/library/webserver/documents/,/var/www/html,/etc/nginx/html, and so on) and run the command:

composer create-project laravel/laravel learnlaravel5

Then, wait a moment, a folder called Learnlaravel5 appears in the current directory.

Then configure the Web site root directory as learnlaravel5/public.

Using your browser to access your configured address, you will see the following screen (my locally configured address is http://fuck.io:88 ):

2. Experience the Auth system and complete the installation

--After the process above, the installation of Laravel 5 was successful?

--No O (╯-╰) o

learnlaravel5/app/Http/routes.php To view the code for a route file:

Route::Get(‘/‘,' [Email protected] ');Route::Get( ' home '  [email  Protected] '  ; Route::controllers  ([ ' auth ' =>  ' Auth\authcontroller '  ' password ' = >  ' Auth\passwordcontroller ' ,])               

Follow the clues in the code, let us visit the http://fuck.io:88/home (please replace the domain name), the results unexpectedly jump to the landing page?

Yes, Laravel has a Auth system with out-of-the-box, even the page has been written.

Let us feel free to enter the email and password, click Login, you are likely to get the following screen (MAC or Linux):

Why blank? Using the developer tool, the status code for this request is 500, why?

Because the learnlaravel5/storage directory does not have 777 permissions.

Execute shell command:

cd learnlaravel5sudo chmod -R 777 storage

Re -access Http://fuck.io:88/home , feel free to enter the mailbox and password if you get the following screen:

Then congratulations to you ~ Laravel 5 Installation Success!

Do not want to configure the image of the classmate, you can use the Laravel world very famous Ang Sul to install the artifact: Https://github.com/overtrue/latest-laravel

3. Database Setup and Migration

Laravel 5 to change the location of the database configuration learnlaravel5/.env , open this file, edit the following four items, modify the correct information:

DB_HOST=localhostDB_DATABASE=laravel5DB_USERNAME=rootDB_PASSWORD=password

It is recommended to create a new database named Laravel5, which is recommended for direct operation with the root account for easy learning.

Laravel has prepared the Auth section of the migration for us, run the following command to perform the database migration operation:

php artisan migrate

The results are as follows:

If you run a command error, check the database connection settings.

Now that the database migration is complete, you can open http://fuck.io:88/home and happily try to register and log in.

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 sentence: Goose sister English!

Run the command:

php artisan make:model Articlephp artisan make:model Page

In the Laravel 4 era, we used the Generator plugin to create a new Model. Now, Laravel 5 has integrated Generator into the Artisan.

Now, Artisan helped us learnlaravel5/app/ Create two files Article.php and Page.php This is the two model class, they all inherit the model class provided by Laravel eloquent and all under the \App namespace. It is necessary to emphasize that creating a file from the command line is no different from creating a file yourself, and you can try to create these two Model classes yourself.

Model is the M in MVC, which translates into models and is responsible for interacting with the database. In eloquent, each table in the database corresponds to a Model class (which can, of course, correspond to multiple).

If you turn from the other frame, may be here a piece of the Model part is not suitable, no way, because eloquent is too powerful, really nothing to do, inherit the eloquent class can achieve a lot of many functions.

If you want to learn more about eloquent, you can read the series: in-depth understanding of Laravel eloquent (a)--basic concepts and usage

Next proceed to the database migration of the article and Page classes corresponding to the articles tables and pages table, into the learnlaravel5/databases/migrations folder.

Modify in ***_create_articles_table.php:

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:

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; })     

Then execute the command:

php artisan migrate

After the success, the tables and pages tables already appear in the database, go to see it ~

5. Database Population seeder

learnlaravel5/databases/seeds/ under new PageTableSeeder.php file, the content is as follows:

<?phpUseIlluminate\database\seeder;UseApp\page;ClassPagetableseederExtendsSeeder{PublicfunctionRun(){Db::Table(' Pages ')->Delete();For($i=0;$i<10;$i++){Page::Create([' Title '=>' Title '.$i ' slug ' =>  ' first-page '   ' body ' =>  ' Body ' . $i  ' user_id ' => 1]} }}    /span>                

Then modify the following in the same level directory DatabaseSeeder.php :

// $this->call(‘UserTableSeeder‘);

This sentence is

$this->call(‘PageTableSeeder‘);

Then run the command to populate the data:

composer dump-autoloadphp artisan db:seed

Go to the Pages table, is there more than 10 rows of data?

Tutorial (a) code snapshot: Https://github.com/johnlui/Learn-Laravel-5/archive/tutorial_1.zip

Laravel 5 Series Introductory Tutorial (i) "Laravel course for the Chinese"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.