How to extend Illuminate\Routing\Route in laravel?

眉间皱痕 提交于 2019-12-24 07:59:39

问题


I want extend Illuminate\Routing\Route and use it in this way:

request()->someCustomFunction();

any suggests ?


回答1:


You can write your own class that extends Illuminate\Routing\Route and the n in your service provider you can bind it like this:

public function register()
{
    $this->app->bind('Illuminate\Routing\Route', 'YourClassThanExtendsRoute');
 }

This should works.




回答2:


The solution is more a hack since Laravel 5 where it became more difficult to extend the default router. All you need is to add the following into your bootstrap file.

$app->singleton('router', \App\Services\Router::class);

Your new router stored in App/Services/Router.php will look like:

namespace App\Services;

class Router extends \Illuminate\Routing\Router
{
    public function someCustomFunction() {
    }
}

Then you will be able to do:

Router::someCustomFunction();

This solution was discussed in Laravel forums here



来源:https://stackoverflow.com/questions/39915619/how-to-extend-illuminate-routing-route-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!