ITPub博客

首页 > IT职业 > IT图书 > laravel 路由基本知识

laravel 路由基本知识

IT图书 作者:jghos 时间:2021-09-09 09:06:12 0 删除 编辑

1 普通路由
//get请求
Route::get($uri, $callback);
//响应所有请求
Route::any('add', $callback);
//传入参数
Route::get('good/{id}', function ($id) {
return 'Good '.$id;
});
//传入多个参数
Route::any('num/{num}/price/{price}', function ($num, $price) {
return true;
});
//请求控制器
Route:any('good/index', 'GoodController@index');
//请求控制器 加上别名
Route:any('good/index', array('uses' => 'GoodController@index', 'as' => 'goods_index'));
//路由分组
Route::group(array('prefix' => 'admin', 'namespace' => 'Admin'), function(){
Route::get('login', 'IndexController@login');
Route::get('index', 'IndexController@index');
});

二 控制路由
假设没有控制器路由的话,每个方法我们都要建立一个路由。有了控制器路由,我们每一个控制器只要建立一个路由。
Route::controller('/', 'IndexController');

get请求访问 http://test.com/ 就访问getIndex方法
post请求访问 http://test.com/ 就访问postIndex方法
get请求访问 http://test.com/detail 就访问getDetail方法

三 资源路由
首先要了解RestFul Api的标准。
Route::resource('good', 'GoodController');
请求方法 请求URI 对应的控制器方法 代表的意义
GET /good index 索引/列表
GET /good/create create 创建(显示表单)
POST /good store 保存你创建的数据
GET /good/{id} show 显示对应id的内容
GET /good/{id}/edit edit 编辑(显示表单)
PUT/PATCH /good/{id} save 保存你编辑的数据
DELETE/good/{id} destroy 删除

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4827/viewspace-2808083/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-10-08

  • 博文量
    84
  • 访问量
    645