Laravel 路由的使用

 

 

 

创建 Route:

/ routes / web.php

 

创建controller:

php artisan  make:controller  ArticleController

 

创建migration 建表:

php artisan make:migration create_articles_table  --create=articles

 

创建Model:

php artisan make:model Article (也可以带目录例如:  front / Article  ,默认路径在 app\    )

 

 

路由设置:

 

Route::get(' / ' , function(){

return view(' / article / index');

});

 

Route::get( '/index' , 'ArticleController@articel_list' );

Route::get();

 

 

路由 + controller@method

  • Route::get('/article_list' , 'ArticleController@getArticleList'); 
  • Route::post('/post','PostController@insert');
  • Route::post('/App/http/controllers/post','PostController@update');
  • Route::match(['get','post'], '/get', 'get@find');
  • Route::any('/article/redirect','ArticleController@redirect');

 

- 路由带参数
    • Route::get('/getFile/{id}',"getFile@gets");
    • Route::get('/getFile/{id?}',"getFile@gets");

 

     路由绑定:

类型声明了 Eloquent 模型 ,  App\Article , 对应的实例 $article 匹配的是路由片段中{article} ,

laravel 会在自动注入和请求uri 中 id 对应的用户的实例。

 

Route::get('/getArticle/{ article }', 'ArticleController@getArticleContent' ); 

public function getArticleContent(Article $article )

{

$article->id ... .... 

} //直接将对象带过去用

 

- 路由分组 group

profix 前缀
Route::group(['profix'=>'/posts'] , function(){
Route::get('/one' , 'post@one');

});


- 路由组中间件 进行过滤
Route::group(['middleware'=>'auth'], function(){
Route::get('/' , function(){  });
Route::get('/article', 'ArticleController@getArticle');
});



- 对路由参数的全局约束
http/provider /RouteServiceProvider.php
public function boot()
{
Route::pattern('id',[0-9]+);
parent::boot();
}

 


laravel-china 文档:
http://d.laravel-china.org/docs/5.4/routing

 

 

  路由分组:

    prefix ,  middleware,  namespace ,  domain

    路由前缀 , 中间件 , 命名空间, 子域名绑定

 

  - 路由

get  post put delete patch options
 Restful 服务中 post put patch 区别
http://blog.csdn.net/iefreer/article/details/10414663

 

  路由参数获取:

    $route = Route::currentRoute();

    $name = Route::currentRouteName();

    $action = Route::currentRouteAction();

  

  

 

posted @ 2017-08-19 18:22  silvercell  阅读(917)  评论(0)    收藏  举报