Getting Started with Laravel 5 Framework (1), Getting Started with Laravel Framework_PHP Tutorial

WBOY
Release: 2016-07-13 09:57:33
Original
1051 people have browsed it

Getting started with the Laravel 5 framework (1), getting started with the laravel framework

Laravel 5 Chinese documentation:

1. http://laravel-china.org/docs/5.0

2. http://www.golaravel.com/laravel/docs/5.0/

Default conditions

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

This article is not recommended for people who don’t understand PHP and MVC programming at all. This article is not a “follow me step by step” tutorial. This article requires you to put a certain amount of effort into solving some hidden tasks, large or small, in order to truly understand the running logic of Laravel.

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

After the mirror configuration is completed, 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 command:

composer create-project laravel/laravel learnlaravel5
Copy after login

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

Then configure the website root directory to learnlaravel5/public.

If you don’t know how to configure it, it is recommended to learn how to configure it. There is a lot of information online. If you give up, you can configure line 29 of 'url' => 'http://localhost' to your subdirectory address. Note that it must be configured until ***/learnlaravel5/public.

Use a browser to access the address you configured, and you will see the following screen (the address I configured locally is http://fuck.io:88 ):


2. Experience the Auth system and complete the installation

——After the above process, Laravel 5 was successfully installed?

—— No o(╯□╰)o

View the code of 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',
]);
Copy after login

Following the clues in the code, let us visit http://fuck.io:88/home (please replace the domain name yourself), but the result actually jumps to the login page?


Yes, Laravel comes with an out-of-the-box Auth system, and even the pages have been written.

Let us enter our email address and password at will, click to log in, and you will most likely get the following screen (under Mac or Linux):


Why is it blank? Use the developer tools to check that the status code of 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
Copy after login

Revisit http://fuck.io:88/home and enter your email and password as you like. If you get the following screen:


Congratulations~ Laravel 5 is successfully installed!

Students who don’t want to configure mirrors can use the installation artifact created by An Zhengchao, a very famous person in the Laravel industry: https://github.com/overtrue/latest-laravel

3. Database creation and migration

Laravel 5 changed the database configuration location to `learnlaravel5/.env`, open this file, edit the following four items, and change it to the correct information:

DB_HOST=localhost

DB_DATABASE=laravel5

DB_USERNAME=root

DB_PASSWORD=password
Copy after login

It is recommended to create a new database named laravel5. For the convenience of learning, it is recommended to use the root account to operate directly.

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

php artisan migrate
Copy after login
Copy after login

The results obtained are as follows:


If you get an error when running the command, please check the database connection settings.

At this point, the database migration has been completed. You can open http://fuck.io:88/home and happily try to register and log in.

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 girl is the best!

Run the command:

php artisan make:model Article

php artisan make:model Page
Copy after login

> In the Laravel 4 era, we used the Generator plug-in to create new models. Now, Laravel 5 has integrated Generator into Artisan.

现在,Artisan 帮我们在 `learnlaravel5/app/` 下创建了两个文件 `Article.php` 和 `Page.php`,这是两个 Model 类,他们都继承了 Laravel Eloquent 提供的 Model 类 `Illuminate\Database\Eloquent\Model`,且都在 `\App` 命名空间下。这里需要强调一下,用命令行的方式创建文件,和自己手动创建文件没有任何区别,你也可以尝试自己创建这两个 Model 类。

Model 即为 MVC 中的 M,翻译为 模型,负责跟数据库交互。在 Eloquent 中,数据库中每一张表对应着一个 Model 类(当然也可以对应多个)。

如果你从其他框架转过来,可能对这里一笔带过的 Model 部分很不适应,没办法,是因为 Eloquent 实在太强大了啦,真的没什么好做的,继承一下 Eloquent 类就能实现很多很多功能了。

如果你想深入地了解 Eloquent,可以阅读系列文章:Laravel 5框架学习之Eloquent 关系

接下来进行 Article 和 Page 类对应的 articles 表和 pages表的数据库迁移,进入 `learnlaravel5/database/migrations` 文件夹。

在 ***_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();
});
Copy after login

在 ***_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();
});
Copy after login

然后执行命令:

php artisan migrate
Copy after login
Copy after login

成功以后, tables 表和 pages 表已经出现在了数据库里,去看看吧~

5. 数据库填充 Seeder

在 `learnlaravel5/database/seeds/` 下新建 `PageTableSeeder.php` 文件,内容如下:

<&#63;php

use 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,
   ]);
  }
 }

}
Copy after login

然后修改同一级目录下的 `DatabaseSeeder.php`中:

// $this->call('UserTableSeeder');
Copy after login

这一句为

$this->call('PageTableSeeder');
Copy after login

然后运行命令进行数据填充:

composer dump-autoloadphp artisan db:seed
Copy after login

去看看 pages 表,是不是多了十行数据?

本教程示例代码见:https://github.com/johnlui/Learn-Laravel-5

大家在任何地方卡住,最快捷的解决方式就是去看我的示例代码。

以上所述就是本文的全部内容了,希望能够对大家学习Laravel5框架有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/981349.htmlTechArticleLaravel 5 框架入门(一),laravel框架入门 Laravel 5 中文文档: 1. http://laravel-china.org/docs/5.0 2. http://www.golaravel.com/laravel/docs/5.0/ 默认条件 本文...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template