Support the ongoing development of Laravel.io →
posted 10 years ago
Requests
Last updated 2 years ago.
0

If you specify your route like

Route::get('/profile/{param1}/{param2}', function($param1,$param2) {
	return "Params {$param1} {$param2}";
});

these URL is valid

www.domain.com/profile/abc/def

and these won't work

www.domain.com/profile/abc/

www.domain.com/profile/abc/def/ghi

You can even specify params as optional by appending ? like

Route::get('/profile/{param1?}/{param2?}', function($param1=null,$param2=null) {
	return "Params {$param1} {$param2}";
});

In this case this URL will be valid (too)

www.domain.com/profile

Last updated 2 years ago.
0

Yes, I agree, but I am currently using Route::controller instead of Route::get(), I have around 6 methods per controller and I don't want to have 500 lines of code inside my routes.php with

Route:;get("/profil/update Route::post("/profil/update Route::get("/profil/delete Route::post("/profil/delete

That's the reason why I was trying to find a solution with the Route::controller()

Last updated 2 years ago.
0

Laravel add automatically five optional parameters to your route like

profile/index/{one?}/{two?}/{three?}/{four?}/{five?}

You could define a before filter in your constructor and test for the existence of the parameter you don't want to have and then thrown an 404:

So if e.g. you don't want to have a third parameter you can try this:

public function __construct()
{
	$this->beforeFilter(function($route) {
		$param = $route->getParameter('three');
		if ( ! empty($param) )
		{
			App::abort(404);			
		}
	});
}

You also have the possibility to define this filter globally.

BTW: I was also the guy who was too lazy to write out all the route because I was used to the magic way of CodeIgniter and Kohana but this article form Phil Sturgeon made me think twice:

http://philsturgeon.co.uk/blog/2013/07/beware-the-route-to-evil

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.