Laravel 5 Framework entry (i) _php instance

Source: Internet
Author: User
Tags auth generator 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 configuration perfect PHP + MySQL running environment, know the basic knowledge that PHP website runs. After you follow this tutorial, you will get a simple blogging system that contains logins, and 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+

This article does not recommend people who are completely ignorant of PHP and MVC programming to learn. This article is not a "step-by-Step with Me" tutorial. This article requires you to pay a certain mind to solve some or large or small hidden tasks, so as to truly understand the purpose of laravel running logic.

1. Installation

Many people were stopped at the first step of learning Laravel, installed. This is not because of the complexity of the tutorial, but because of "known reasons". Here I recommend a composer full volume Chinese mirror image: http://pkg.phpcomposer.com/. It is recommended that you configure the "Modify Composer Profile" method.

Once the mirror 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, etc.) 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 site root directory as learnlaravel5/public.

If you do not configure, suggest to learn to configure, online data a lot. If you abandon yourself, you can put the 29th line ' url ' => ' http://localhost ', configured into your subdirectory address, note, to be configured to ***/learnlaravel5/public.

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


2. Experience Auth system and complete installation

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

--No O (╯-╰) o

To view the code for the routing 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 result unexpectedly jump to the landing page?


Yes, laravel the Auth system with the Open box, even the page has been written.

Let us enter the mailbox and password randomly, click Login, you are likely to get the following screen (under Mac or Linux):


Why blank? With 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 Learnlaravel5

sudo chmod-r 777 storage

Re-access the Http://fuck.io:88/home and enter your mailbox and password at random, if you get the following screen:


So congratulations ~ 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 do installation artifact: Https://github.com/overtrue/latest-laravel

3. Database establishment and migration

Laravel 5 Changed the location of the database configuration to ' learnlaravel5/.env ', opened the file, edited the following four items, modified to the correct information:

Db_host=localhost

db_database=laravel5

db_username=root

Db_password=password

It is recommended to create a new database named Laravel5, which is recommended to use the root account directly for easy learning.

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

PHP Artisan Migrate

The results obtained are as follows:


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

At this point, the database migration has been completed, you can open http://fuck.io:88/home happy to try to register, login.

4. Model Models

Next we will touch the most powerful part of Laravel, eloquent ORM, the place to really improve productivity, borrow Cook's word: Goose sister English!

Run the command:

PHP artisan make:model Article

php artisan Make:model Page

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

Now, Artisan helps us create two files ' article.php ' and ' page.php ' under ' learnlaravel5/app/', which is the two model class that inherits the model class provided by Laravel eloquent ' Illuminate\database\eloquent\model ', and all under the ' \app ' namespace. Here you need to emphasize that creating a file on a command-line basis is no different from creating your own files manually, and you can try creating these two Model classes yourself.

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

If you turn around from other frames, you may have a bit of a belt over the Model section is very uncomfortable, there is no way, because the eloquent is too powerful, really nothing good to do, inherit eloquent class can achieve a lot of many functions.

If you want to learn more about eloquent, you can read a series of articles: the eloquent relationship of Laravel 5 Framework Learning

Next proceed to the Article and Page class corresponding to the articles table and pages table database migration, into 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 ();
});

And then execute the command:

PHP Artisan Migrate

After the success, tables and pages table has been in the database, to see it ~

5. Database Fill Seeder

Under ' learnlaravel5/database/seeds/', create a new ' pagetableseeder.php ' file that reads as follows:

<?php use

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

To look at the pages table, is not more than 10 rows of data?

For this tutorial sample code see: Https://github.com/johnlui/Learn-Laravel-5

The quickest way to get stuck everywhere is to look at my sample code.

The above mentioned is the whole content of this article, hope to be able to learn Laravel5 frame to be helpful to everybody.

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.