## 第4節 路由
~~~
git checkout -b route
~~~
### 簡介
Laravel 5.2 中路由的位置:`D:\wamp\www\newblog.com\app\Http\routes.php`
~~~
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::auth();
Route::get('/home', 'HomeController@index');
~~~
路由:<http://d.laravel-china.org/docs/5.2/routing>
### 4.1 基礎路由
~~~
Route::get('/', function () {
return view('welcome');
});
~~~
最基本的 Laravel 路由僅接受 URI 和一個閉包。
上面自帶的路由就是訪問放在的根目錄時,返回一個 `resources\views\welcome.blade.php` 視圖文件。
~~~
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">Welcome</div>
<div class="panel-body">
Your Application's Landing Page.
</div>
</div>
</div>
</div>
</div>
@endsection
~~~
關于視圖的內容,我們在后面的章節再詳述吧!
是不是可以看到 “Your Application's Landing Page.”內容輸出了?

其實這種閉包路由使用的不多,我們使用的更多的是控制器路由。
~~~
Route::get('/home', 'HomeController@index');
~~~
關于 `Route::auth();` 這條路由,其實是系統自定義好的。在:`D:\wamp\www\newblog.com\vendor\laravel\framework\src\Illuminate\Routing\Router.php`
~~~
public function auth()
{
// Authentication Routes...
$this->get('login', 'Auth\AuthController@showLoginForm');
$this->post('login', 'Auth\AuthController@login');
$this->get('logout', 'Auth\AuthController@logout');
// Registration Routes...
$this->get('register', 'Auth\AuthController@showRegistrationForm');
$this->post('register', 'Auth\AuthController@register');
// Password Reset Routes...
$this->get('password/reset/{token?}', 'Auth\PasswordController@showResetForm');
$this->post('password/email', 'Auth\PasswordController@sendResetLinkEmail');
$this->post('password/reset', 'Auth\PasswordController@reset');
}
~~~
看下:` $this->get('login', 'Auth\AuthController@showLoginForm');` 這條路由訪問的頁面效果:

它實際上訪問的是 `AuthController` 控制器的 `showLoginForm` 方法。
*\vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php*
~~~
public function showLoginForm()
{
$view = property_exists($this, 'loginView')
? $this->loginView : 'auth.authenticate';
if (view()->exists($view)) {
return view($view);
}
return view('auth.login');
}
~~~
是不是可以感受到路由的強大之處?好了,這里先不要糾結,后面也就慢慢可以理解了。
關于更多的路由知識,大家可以借助手冊進一步了解,如路由群組、路由別名等概念,這里就不做詳述了!
### 4.2 使用 artisan 查看路由列表
命令:
~~~
php artisan route:list
~~~
