1

So i have checked out PHP - Routing with Parameters in Laravel and Laravel 4 mandatory parameters error

However using what is said - I cannot seem to make a simple routing possible unless im not understanding how filter/get/parameters works.

So what I would like to do is have a route a URL of /display/2 where display is an action and the 2 is an id but I would like to restrict it to numbers only.

I thought

Route::get('displayproduct/(:num)','SiteController@display');
Route::get('/', 'SiteController@index');

class SiteController extends BaseController {

public function index()
{

    return "i'm with index";
}

public function display($id)
{
    return $id;
}
}

The problem is that it throws a 404 if i use

Route::get('displayproduct/{id}','SiteController@display');

it will pass the parameter however the URL can be display/ABC and it will pass the parameter. I would like to restrict it to numbers only.

I also don't want it to be restful because index I would ideally would like to mix this controller with different actions.

2 Answers 2

8

Assuming you're using Laravel 4 you can't use (:num), you need to use a regular expression to filter.

Route::get('displayproduct/{id}','SiteController@display')->where('id', '[0-9]+');
Sign up to request clarification or add additional context in comments.

4 Comments

Correct, all constraints are done using regular expressions, using {id} will allow anything to match. Take a look at laravel.com/docs/routing#route-parameters
Code Happy is for Laravel 3, check out Code Bright (for Laravel 4) codebright.daylerees.com
Wow...if only i ran into this - that site needs to be up in the top 10 search results to avoid headaches. -tks for the link
@dragoon What If I want to provide choices for id, like only value 1,3 & 5 can be passed? How can I do that?
3

You may also define global route patterns

Route::pattern('id', '\d+');

How/When this is helpful?

Suppose you have multiple Routes that require a parameter (lets say id):

Route::get('displayproduct/{id}','SiteController@display');
Route::get('editproduct/{id}','SiteController@edit');

And you know that in all cases an id has to be a digit(s).

Then simply setting a constrain on ALL id parameter across all routes is possible using Route patterns

Route::pattern('id', '\d+');

Doing the above will make sure that all Routes that accept id as a parameter will apply the constrain that id needs to be a digit(s).

3 Comments

Sorry - I don't understand that one - and I haven't seen anyone use that one yet
Sorry for being late on looking into your answer - it is effective if the pattern is an actual pattern. The Where is prefered if its digits because you can restrict it to be digits. Its about the equivalent of doing a preg_reg match comparing to is_numeric - is_numeric yields faster results
@azngunit81 I think this is a better option if you want to re-use a pattern - for the same reason for which we use functions. Also it does not matter what pattern you want to match against, this is also using regex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.