重写 Laravel Router 类,路由能够打印,访问却提示 404
继承框架Router类,并重写apiResource方法
namespace App\Overload\Routing;
use Illuminate\Routing\Router as CoreRouter;
class Router extends CoreRouter
{
public function apiResource($name, $controller, array $options = [])
{
$only = ['ls', 'detail', 'add', 'edit', 'delete','editBatch','deleteBatch'];
if (isset($options['except'])) {
$only = array_diff($only, (array) $options['except']);
}
return $this->resource($name, $controller, array_merge([
'only' => $only,
], $options));
}
重载 ResourceRegistrar类,并重写addResourceXXX的相关方法
namespace App\Overload\Routing;
use Illuminate\Routing\ResourceRegistrar as CoreResourceRegistrar;
class ResourceRegistrar extends CoreResourceRegistrar
{
protected $resourceDefaults = ['ls', 'addPage', 'add', 'detail', 'editPage', 'edit', 'delete', 'editBatch', 'deleteBatch'];
protected static $verbs = [
'create' => 'addPage',
'edit' => 'editPage'
];
protected function addResourceLs($name, $base, $controller, $options)
{
$uri = $this->getResourceUri($name);
$action = $this->getResourceAction($name, $controller, 'ls', $options);
return $this->router->get($uri, $action);
}
protected function addResourceAddPage($name, $base, $controller, $options)
{
$uri = $this->getResourceUri($name).'/'.static::$verbs['create'];
$action = $this->getResourceAction($name, $controller, 'editPage', $options);
return $this->router->get($uri, $action);
}
//以下代码省略
}
替换系统的Router和ResourceRegistrar
AppServiceProvider中替换原来的router和ResourceRegistrar
class AppServiceProvider extends ServiceProvider { /** * Register any application services. * * @return void */ public function register() { $this->app->singleton('router',function($app){ return new \App\Overload\Routing\Router($app['events'],$app); }); } /** * Bootstrap any application services. * @return void */ public function boot() { $this->app->bind(\Illuminate\Routing\ResourceRegistrar::class, function (){ return new \App\Overload\Routing\ResourceRegistrar($this->app['router']); }); } }
HttpKernel中取代原来的Router
namespace App\Http; use Illuminate\Foundation\Http\Kernel as HttpKernel; use Illuminate\Contracts\Foundation\Application; use App\Overload\Routing\Router; class Kernel extends HttpKernel { /** * Create a new HTTP kernel instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @param \App\Overload\Routing\Router $router * @return void */ public function __construct(Application $app, Router $router) { parent::__construct($app,$router); } //代码略。。。 }
目前取代系统的Router完毕,并且在系统中能够正常打印被取代的Router(如有替换遗漏,请指出,谢谢!)
在web.php定义一条路由: Route::apiResource(‘/module_example’,’ExampleWeb’);
通过php artisan route:list 能够正常打印出路由
但是在浏览器访问却提示404 NOT FOUND
问:
1.请问如何才能正确替换系统的Router呢?
2.如不能替换,请问如果才能重写Router上的方法呢?谢谢!
推荐文章: