Laravel 5 framework entry (1): laravel framework entry

Source: Internet
Author: User

Laravel 5 framework entry (1): laravel framework entry

Laravel 5 Chinese document:



Default Condition

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 +

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.

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 composer Chinese Image: /. We recommend that you use the "Modify composer configuration file" method.

After the image is configured, switch to the directory where you want to place the website (such as C: \ wwwroot,/Library/WebServer/Documents/,/var/www/html,/etc/nginx/html, etc.), run the following command:

composer create-project laravel/laravel learnlaravel5

Then, wait a moment and a folder named learnlaravel5 will appear in the current directory.

Configure the root directory of the website as learnlaravel5/public.

If you do not configure it, we recommend that you learn how to configure it. There is a lot of information on the Internet. If you discard it, you can set the 29th line 'url' => 'HTTP: // localhost' to your subdirectory address. Note, always configure it to ***/learnlaravel5/public.

Access the address you configured with your browser and you will see the following screen (the address I configured locally is 88 ):

2. Experience the Auth system and complete the installation

-- After the above process, is Laravel 5 successfully installed?

-- No o (Executor □executor) o

View the Routing File 'learnlaravel5/app/Http/routes. php' code:

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 access the 88/home (please replace the domain name), the results jump to the login page?

Yes, Laravel comes with the out-of-the-box Auth system, which has been written on all pages.

Let's enter the email address and password at will, and click log on. You will probably get the following screen (Mac or Linux ):

Why is it blank? Check with developer tools. The Request status code is 500. Why?

Because the 'learnlaravel5/store' directory does not have 777 permissions.

Run the shell command:

cd learnlaravel5sudo chmod -R 777 storage

Re-access 88/home, feel free to enter your mailbox and password if you get the following picture:

Congratulations ~ Laravel 5 is successfully installed!

Do not want to configure the image of the students, you can use Laravel industry very famous anzheng Chao engaged in installation artifacts:

3. database creation and migration

Laravel 5 changed the database configuration to 'learnlaravel5/. env'. Open this file, edit the following four items, and modify them to the correct information:


We recommend that you create a database named laravel5. For the convenience of learning, we recommend that you use the root account for direct operations.

Laravel has prepared the migration of Auth for us. Run the following command to perform database migration:

php artisan migrate

The result is as follows:

If you run the command to report an error, check the database connection settings.

So far, the database migration has been completed, you can open the 88/home happily try to register, login.

4. Model Models

Next we will get into touch with Laravel's most powerful part, Eloquent ORM, where we can really improve productivity by borrowing a sentence from Cook: Goose girl Ying!

Run the following command:

php artisan make:model Articlephp artisan make:model Page

> In the Laravel 4 era, we use the Generator plug-in to create a Model. Now Laravel 5 has integrated Generator into Artisan.

Now, Artisan helps us create two 'Article' files under 'learnlaravel5/app. php' and 'page. php'. These are two Model classes. They all inherit the Model class 'illuminate \ Database \ Eloquent \ model' provided by Laravel Eloquent and all belong to the '\ app' namespace. It should be emphasized that creating a file using the command line method is no different from creating a file manually. You can also try to create these two Model classes by yourself.

Model is M in MVC. It is translated as a Model and is responsible for interacting with the database. In Eloquent, each table in the database corresponds to a Model class (of course, multiple tables can also be matched ).

If you switch from other frameworks, you may not be able to adapt to the Model section mentioned here. It's because Eloquent is so powerful that it really has nothing to do with it, inherit the Eloquent class to implement many functions.

If you want to learn more about Eloquent, read a series of articles: the relationship between Eloquent and Laravel 5 framework learning.

Next, migrate the database of the articles table and pages table corresponding to the Article and Page types to the 'learnlaravel5/database/migrations 'folder.

In *** _ create_articles_table.php, modify:

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:

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 run the following command:

php artisan migrate

After the operation is successful, the tables Table and pages table have already appeared in the database ~

5. Fill the database with Seeder

Create the 'pagetableseeder. php' file under 'learnlaravel5/database/seeds/'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 < 10; $i++) {   Page::create([    'title'  => 'Title '.$i,    'slug'  => 'first-page',    'body'  => 'Body '.$i,    'user_id' => 1,   ]);  } }}

Modify 'databaseseeder. php' in the same level directory:

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

This sentence is


Then run the command to fill in the data:

composer dump-autoloadphp artisan db:seed

Check out the pages table. Is there 10 more rows of data?

For sample code in this tutorial, see:

Everyone gets stuck anywhere. The quickest solution is to view my sample code.

The above is all the content of this article, hoping to help you learn the Laravel5 framework.

Related Article

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