Laravel 5 Introduction to the framework (i) _php

Source: Internet
Author: User
Keywords Laravel Introductory tutorial Getting Started with Laravel LA
Tags php website
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.

If you do not configure, it is recommended to learn to configure, online data a lot. If you abandon yourself, you can put the 29th line ' url ' = ' http://localhost ', configured as your subdirectory address, note, to always configure to ***/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

To view the code for the route file ' learnlaravel5/app/http/routes.php ':

Route::get ('/', ' welcomecontroller@index '); Route::get (' Home ', ' homecontroller@index '); 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 realm very famous Ang Sul to engage in the installation artifact: Https://github.com/overtrue/latest-laravel

3. Database Setup and Migration

Laravel 5 Change the location of the database configuration to ' learnlaravel5/.env ', open this file, edit the following four items and modify it to 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

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

Now, Artisan helped us create two files under ' learnlaravel5/app/', ' article.php ' and ' page.php ', which are two model classes that inherit the model class provided by Laravel eloquent. ' Illuminate\database\eloquent\model ', and are 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: Laravel 5 Framework Learning eloquent relationship

Next proceed to the database migration of the article and Page classes corresponding to the articles table and pages table, and go to the ' learnlaravel5/database/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

Under ' learnlaravel5/database/seeds/', create a new ' pagetableseeder.php ' file with the following content:

<?phpuse illuminate\database\seeder;use App\page;class Pagetableseeder extends Seeder {public Function run () {  Db::table (' pages ')->delete ();  for ($i =0; $i <, $i + +) {   page::create ([    ' title '  = ' title ') $i,    ' slug '  = ' first-page ',    ' body '  = ' body '. $i,    ' user_id ' = 1,   ]);  }}

Then modify the ' databaseseeder.php ' in the same level directory:

$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?

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.

The above mentioned is the whole content of this article, I hope to be able to learn LARAVEL5 framework to help you.

  • 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.