当前位置: 代码迷 >> 综合 >> laravel中的资源(resource)路由的使用方法
  详细解决方案

laravel中的资源(resource)路由的使用方法

热度:66   发布时间:2024-02-27 20:51:33.0

laravel框架中的路由:

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
Route::match(['get', 'post'], $uri,$callback);
Route::any($uri, $callback);
Route::redirect('/here', '/there', 301);
Route::view('/welcome', 'welcome');                                                                                         Route::resource("/",'IndexController');

刚开始学习laravel框架的时候,只看到有resource路由,但是不知道该怎么用,研究了好久才明白是怎么回事。

首先,我用的是laravel5.5的框架,先在web.php中编写好路由,如:

Route::group(['prefix'=>'admin','namespace'=>'Admin'],function(){Route::resource("/",'IndexController');
});

这段代码其实很好理解。

prefix指的是访问的前缀,namespace则是访问的公共命名空间。

resource('域名/admin/',访问的控制器)  控制器中的方法

 

方法 路径 动作 路由名称
GET /photos index photos.index
GET /photos/create create photos.create
POST /photos store photos.store
GET /photos/{photo} show photos.show
GET /photos/{photo}/edit edit photos.edit
PUT/PATCH /photos/{photo} update photos.update
DELETE /photos/{photo} destroy photos.destroy

当输入的链接地址为:域名/admin/时,默认访问的是indexController中的index方法,而post提交的数据默认的是store方法

 

 

  相关解决方案