Search code examples
laravellaravel-4routesnested-resources

Laravel 4 self relationship nested routes


I'm starting with laravel and I need to do something like this localhost/diretory1/directory2/directory3

Is is possible to set this up like a nested route? At the moment its working like this localhost/directory1 -> localhost/directory2


Solution

  • So what you can do is define one slug route that catches all requests. (Make sure that you define other routes above that one route so the request ends only up in the slug route if nothing else matches)

    Route::any('{slug}', function($slug){
        $directories = explode('/', $slug);
    
        // lookup the directory(ies) in the db, file system, etc 
    
        if(!$exists){
            // when the directories don't exists, it's probably appropriate to throw a 404 Not found error.
            App::abort(404);
        }
    }
    

    What you do in the route function (or a controller if it gets too much code to live in the routes.php) is up to you. I don't know how your application works so I can't help you with that.