Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: any and all routes #39

Closed
DPr00f opened this issue Jan 14, 2013 · 5 comments
Closed

Request: any and all routes #39

DPr00f opened this issue Jan 14, 2013 · 5 comments

Comments

@DPr00f
Copy link
Contributor

DPr00f commented Jan 14, 2013

Well in L3 we could redirect any user request to a custom controller using Route::any('(:any), (:any)/(:all)', 'mycontroller@index');
With the changes in L4 i kinda see this idea lost, so please (re)implement the feature in L4.
I don't know a beautiful alternative to that problem in L4, i guess i could listen for 404 pages, get the values of the URL myself and start over there, but that's an ugly solution, if anyone knows any better ways, please tell me.

@bencorlett
Copy link
Contributor

@DPr00f

Firstly, a 404 in L4 is handled by catching a Symfony\Component\HttpKernel\Exception\HttpNotFoundException so that's how you handle your 404's.

How you do the same in L4 is by appending where() after the declaration of a route, where the first parameter is the named param in the route and the second param is a regular expression which must match true. If it does not, the route is not executed and another match is found (if it exists). For example (don't quote me on syntax errors :P):

<?php

// GET /foo/123/some/other/value/here
// GET /foo/a123/sdf will not work as "a123" is not numeric.
Route::get('foo/{bar}/{baz}', function($bar, $baz)
{
    // $bar == 123
    // $baz == 'some/other/value/here'

})->where('bar', '\d+') // Digits
  ->where('baz', '.*');  // Anything

// You can modify your REGEX to suit:

// GET /foo/a123/asdf
// GET /foo/z123/asdf
// GEt /foo/df123/asdkfjsd
Route::get('foo/{bar}/{baz}', function($bar, $baz)
{

})->where('bar', '[a-z]{1,2}\d+') // 1-2 Letters + Digits
  ->where('baz', '.*');  // Anything

Have a look at http://symfony.com/doc/2.0/components/routing/introduction.html for more as L4 uses the Symfony routing as it's backbone.

Also, check out http://gskinner.com/RegExr/ for a really intuitive way to build up regular expressions :)

@DPr00f
Copy link
Contributor Author

DPr00f commented Jan 14, 2013

That's it, ty very much. +1

Route::any('{all}', function(){
    return 'It Works';
})->where('all', '.*');

@bencorlett
Copy link
Contributor

No problem, glad I could help.

I was a bit disappointed to see (:all) go at first until I saw the potential of this solution!

I'd recommend closing the issue now :)

@niallobrien
Copy link

This is covered in the Laravel docs at four.laravel.com is it not? Also, @jasonlewis has a great blog post on this issue.

@DPr00f DPr00f closed this as completed Jan 24, 2013
@francoishill
Copy link

None of the above works in Laravel 4.0. Here is the solution:

Route::any('{firstPart}/{rest}', function($firstPart, $rest){
    return Response::make($firstPart . ", " . $rest);
})->where('firstPart', '[^/]*')->where('rest', '.*');

joelharkes pushed a commit to joelharkes/framework_old that referenced this issue Mar 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants