Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, January 2, 2016

Laravel - Using (:any?) wildcard for ALL routes?

Laravel - Using (:any?) wildcard for ALL routes?


I am having a bit of trouble with the routing.

I'm working on a CMS, and i need two primary routes. /admin and /(:any). The admin controller is used for the route /admin, and the view controller should be used for anything else than /admin. From the view controller, i will then parse the url and show the correct content.

This is what i have:

Route::get(array('admin', 'admin/dashboard'), array('as' => 'admin', 'uses' =>'admin.dashboard@index'));  Route::any('(:any)', 'view@index');  

The first route works, but the second one doesn't. I played around with it a little bit, and it seems if i use (:any) without the question mark, it only works if i put something after /. If i do put the question mark there, it doesn't work at all.

I want all of the following routes to go to view@index:

/  /something  /something/something  /something/something/something  /something/something/something/something  ...etc...  

Is this possible without hardcoding a bunch of (:any?)/(:any?)/(:any?)/(:any?) (which i don't even know works)?

What's the best way to go about this?

Answer by William Cahill-Manley for Laravel - Using (:any?) wildcard for ALL routes?


Edit: There has been some confusion since the release of Laravel 4 regarding this topic, this answer was targeting Laravel 3.

There are a few ways to approach this.

The first method is matching (:any)/(:all?):

Route::any('(:any)/(:all?)', function($first, $rest=''){      $page = $rest ? "{$first}/{$rest}" : $first;      dd($page);  });  

Not the best solution because it gets broken into multiple parameters, and for some reason (:all) doesn't work by itself (bug?)

The second solution is to use a regular expression, this is a better way then above in my opinion.

Route::any( '(.*)', function( $page ){      dd($page);  });  

There is one more method, which would let you check if there are cms pages even when the route may have matched other patterns, provided those routes returned a 404. This method modifies the event listener defined in routes.php:

Event::listen('404', function() {      $page = URI::current();      // custom logic, else      return Response::error('404');  });  

However, my preferred method is #2. I hope this helps. Whatever you do, make sure you define all your other routes above these catch all routes, any routes defined after will never trigger.

Answer by Chris Schalenborgh for Laravel - Using (:any?) wildcard for ALL routes?


Thanks for the solution William. However methods 1 & 2 aren't working anymore Laravel 4, and in order to use solution #3 in Laravel 4 you will have to fire the 404 event in your start/global.php file.

App::error(function(Exception $exception, $code)  {      // i.o. -> this is our catchall!      // http://stackoverflow.com/questions/13297278/laravel-using-any-wildcard-for-all-routes      Event::fire('404');        return View::make('error')->with('exception', $exception)->with('code', $code);        Log::error($exception);  });  

Now we can handle this in our routes.php file:

Event::listen('404', function() {      // url?      $url = Request::path();        // LOGIC HERE        // else      return View::make('error');  });  

Answer by Eelke van den Bos for Laravel - Using (:any?) wildcard for ALL routes?


Hitting a 404 status seems a bit wrong to me. This can get you in all kind of problems when logging the 404's. I recently bumped into the same wildcard routing problem in Laravel 4 and solved it with the following snippet:

Route::any('{slug}', function($slug)  {      //do whatever you want with the slug  })->where('slug', '([A-z\d-\/_.]+)?');  

This should solve your problem in a controlled way. The regular expression can be simplified to:

'(.*)?'  

But you should use this at your own risk.

Edit (addition):

As this overwrites a lot of routes, you should consider wrapping it in an "App::before" statement:

    App::before(function($request) {              //put your routes here      });  

This way, it will not overwrite custom routes you define later on.

Answer by Edmhs for Laravel - Using (:any?) wildcard for ALL routes?


Add this in the end of routes file

App::missing(function($exception)  {      return View::make('notfound');  });  

from http://scotch.io/tutorials/simple-and-easy-laravel-routing

Answer by Andrea for Laravel - Using (:any?) wildcard for ALL routes?


This solution works fine for me with Laravel 5:

Route::get('/admin', function () {      // url /admin    });    Route::get('/{any}', function ($any) {      // any other url, having subfolders also    })->where('any', '.*');  

Lumen

This is for Lumen instead, a little different:

$app->get('/admin', function () use ($app) {    //  });    $app->get('/{any:.*}', function ($any) use ($app) {    //  });  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.