regex - Regular expression route constraint for a resource route -
laravel offers possibility add regular expression constraint route this:
route::get('user/{name}', function($name) { // }) ->where('name', '[a-za-z]+');
it possible create multiple routes resource:
route::resource('photo', 'photocontroller');
i want add regular expression constraint route get /photo/{id}
is possible?
as far know can't may mimic using (route filtering):
public function __construct() { $this->beforefilter('checkparam', array('only' => array('getedit', 'postupdate'))); }
this example of route filtering using constructor , here i've filtering 2 methods (you may use except
or nothing @ all) , declared filter in filters.php
file given below:
route::filter('checkparam', function($route, $request){ // 1 default name first parameter $param1 = $route->parameter('one'); if(!preg_match('/\d/', $param1)) { app::abort(404); // or 1 throw new symfony\component\httpkernel\exception\notfoundhttpexception; } });
here, i'm checking first parameter manually (parameters
method returns array of parameters passed route) , if it's not digit throwing notfoundhttpexception
exception.
you may catch exception registering handler this:
app::missing(function($exception){ // show user friendly message or whatever... });
Comments
Post a Comment