override laravel 5.7 autodiscovered package routes

Viewed 734

I am having trouble allowing users to override my package routes. Because the package is auto discovered, I am unable to do the usual fix for this.

The desired result is if in the routes->web.php a user defines route '/home', and in my package's routes.php file I also have '/home', the web.php version is used.

Let me know what other code I need to be looking at to solve this!

Package Service Provider:

no longer relevant

Solution:

To solve this issue in the manner I intended, (without breaking autodiscover) you will need to check for other routes within the route that generally overrides user routes. For instance, this function here which has the route '/{slug}' generally breaks a users web.php routes like '/userroute'.

My workaround goes like this.

public function page($slug){
    $route = $slug;
    $routes = Route::getRoutes();
    foreach($routes as $r){
      if($r->uri() == $route){
        $name = $r->getActionName();
        if(isset($name)){
          return \App::call('\\'.$name);
        }
      }
    }
    //add route code after

We need to call the route directly, to stop an endless redirect from occurring. This works well for me, because I can control which package routes a user is able to override in his web.php file, however I am sure there must be a faster way to achieve all this.

1 Answers
Related