skip to Main Content

I have a Laravel 5.8 application and I need to add a custom anonymous route to the Laravel’s core file which is Router.php and located in this directory:

/vendor/laravel/framework/src/Illuminate/Routing

I checked the file but since it does not have any map method, so I couldn’t define the route.

So the question is how can I add the route in this case?

2

Answers


  1. What about adding a line like:

    $this->get('/hiddenroute', function(Request $request) { return 'hello, I am an hidden route';}); // $request is optional
    

    as last line in the __construct() method of /vendor/laravel/framework/src/Illuminate/Routing/Route.php ?

    Login or Signup to reply.
  2. Go to this file app/Providers/RouteServiceProvider and add this method:

    protected function mapHiddenRoutes()
        {
            Route::prefix('hidden')
                 ->middleware('web')
                 ->namespace($this->namespace)
                 ->group(function () {
                        Route::get('secret', function () {
                            return ['this is my secret route'];
                        });
                    });
        }
    

    And on the map method, add the newly created method $this->mapHiddenRoutes().

    public function map()
        {
            $this->mapApiRoutes();
    
            $this->mapWebRoutes();
    
            $this->mapHiddenRoutes()
    
            //
        }
    

    Then you can go on your browser and visit {domain}/hidden/secret

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search