Prakhar
Prakhar

Reputation: 2310

Laravel routing issue

I am using laravel framework. I've setup couple of routes that take a parameter like example.com/route/{dir_name}. When I pass in a dir which has childrens it considers it as another route. Is there a way to bypass it?

I'm using this code:

Route::get('/route/something/{path}',array('as'=>'something',function($path){
     return $path;
}));

When I use /route/something/home/user/dev it throws a Symfony\Component\HttpKernel\ Exception\NotFoundHttpException exception.

Upvotes: 0

Views: 69

Answers (2)

borfast
borfast

Reputation: 2274

You can try using constraints on the route parameter with a regular expression.

Route::any('/route/{dir_name}', function ($dir_name) {
    return $dir_name;
})->where('dir_name', '.*');

See the docs section about route parameters, specifically the "Regular Expression Route Constraints" part.

Upvotes: 1

arkhamDev
arkhamDev

Reputation: 1048

You have example.com/route/{dir_name}

I assume your route defined in routes.php

Route::get('/route/{dir_name}', [
    'as' => 'someRoute',
    'uses' => 'SomeController@index'
]);

In your SomeController.php file you would have this method that takes in the {dir_name}

public function index($dir_name) {
return $dir_name;
}

Instead you have the Route

Route::get('/route/something/{path}',array('as'=>'something',function($path){
     return $path;
}));

/route/something/{path}

If you want something in your URL then it has to be in the Route. This is why you're getting the NotFoundHttpException

Upvotes: 0

Related Questions