Answers:
在注册资源之前,只需分别向该方法添加路由:
Route::get('foo/bar', 'FooController@bar');
Route::resource('foo', 'FooController');
Route::get('foo/{id}', ...)
。这会吞噬所有以开头foo
并具有另外一段的路线,包括foo/bar
。
我只是这样做,添加了GET“删除”方法。
创建文件后,只需添加
'AntonioRibeiro\Routing\ExtendedRouterServiceProvider',
到您的app / config.php中的“提供者”
在同一文件中编辑Route别名:
'Route' => 'Illuminate\Support\Facades\Route',
更改为
'Route' => 'AntonioRibeiro\Facades\ExtendedRouteFacade',
并确保这些文件正在自动加载,它们必须位于composer.json中的某个目录中(“自动加载”部分)。
然后,您只需要:
Route::resource('users', 'UsersController');
如果运行,这是结果(请看最后一行)php artisan routes
:
这些是我的源文件:
ExtendedRouteFacade.pas
<?php namespace AntonioRibeiro\Facades;
use Illuminate\Support\Facades\Facade as IlluminateFacade;
class ExtendedRouteFacade extends IlluminateFacade {
/**
* Determine if the current route matches a given name.
*
* @param string $name
* @return bool
*/
public static function is($name)
{
return static::$app['router']->currentRouteNamed($name);
}
/**
* Determine if the current route uses a given controller action.
*
* @param string $action
* @return bool
*/
public static function uses($action)
{
return static::$app['router']->currentRouteUses($action);
}
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'router'; }
}
ExtendedRouter.pas
<?php namespace AntonioRibeiro\Routing;
class ExtendedRouter extends \Illuminate\Routing\Router {
protected $resourceDefaults = array('index', 'create', 'store', 'show', 'edit', 'update', 'destroy', 'delete');
/**
* Add the show method for a resourceful route.
*
* @param string $name
* @param string $base
* @param string $controller
* @return void
*/
protected function addResourceDelete($name, $base, $controller)
{
$uri = $this->getResourceUri($name).'/{'.$base.'}/destroy';
return $this->get($uri, $this->getResourceAction($name, $controller, 'delete'));
}
}
ExtendedRouteServiceProvider.pas
<?php namespace AntonioRibeiro\Routing;
use Illuminate\Support\ServiceProvider;
class ExtendedRouterServiceProvider extends ServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app['router'] = $this->app->share(function() { return new ExtendedRouter($this->app); });
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array('router');
}
}
是的,可能
在我的情况下,我添加了method:data来处理HTTP POST方法中对/data.json的请求。
这就是我所做的。
首先,我们扩展Illuminate \ Routing \ ResourceRegistrar以添加新的方法数据
<?php
namespace App\MyCustom\Routing;
use Illuminate\Routing\ResourceRegistrar as OriginalRegistrar;
class ResourceRegistrar extends OriginalRegistrar
{
// add data to the array
/**
* The default actions for a resourceful controller.
*
* @var array
*/
protected $resourceDefaults = ['index', 'create', 'store', 'show', 'edit', 'update', 'destroy', 'data'];
/**
* Add the data method for a resourceful route.
*
* @param string $name
* @param string $base
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\Route
*/
protected function addResourceData($name, $base, $controller, $options)
{
$uri = $this->getResourceUri($name).'/data.json';
$action = $this->getResourceAction($name, $controller, 'data', $options);
return $this->router->post($uri, $action);
}
}
之后,创建新的ServiceProvider或使用AppServiceProvider。
在方法启动中,添加以下代码:
public function boot()
{
$registrar = new \App\MyCustom\Routing\ResourceRegistrar($this->app['router']);
$this->app->bind('Illuminate\Routing\ResourceRegistrar', function () use ($registrar) {
return $registrar;
});
}
然后 :
添加到您的路线:
Route::resource('test', 'TestController');
通过检查,php artisan route:list
您会发现新方法“数据”
Route::resource('foo', 'FooController');
Route::controller('foo', 'FooController');
尝试一下。给您额外的方法,例如getData()等。。这对我来说可以保持route.php干净
这也很好。无需添加更多路由,只需使用资源控制器的show方法,如下所示:
public function show($name){
switch ($name){
case 'foo':
$this -> foo();
break;
case 'bar':
$this ->bar();
break;
defautlt:
abort(404,'bad request');
break;
}
}
public function foo(){}
publcc function bar(){}
我使用默认设置来抛出自定义错误页面。
::resource
否则您将收到错误消息“没有模型的查询结果”。