fico7489
fico7489

Reputation: 8560

How to extend Illuminate\Routing\Route in laravel?

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

request()->someCustomFunction();

any suggests ?

Upvotes: 4

Views: 3069

Answers (2)

nowox
nowox

Reputation: 29178

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

Upvotes: 1

Filip Koblański
Filip Koblański

Reputation: 10018

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.

Upvotes: 4

Related Questions