php - Pass fixed variable from route to controller in Laravel -


i'm trying pass variable through route controller, have multiple routes (categories) leading same controller i.e.

route::get('/category1/{region}/{suburb?}', 'searchcontroller@search'); route::get('/category2/{region}/{suburb?}', 'searchcontroller@search'); 

making /category1, 2, etc. parameter /{category} not option , don't want make separate controller function each category.

how send first segment of url search controller? i.e. category1 or category2?

at present controller follows:

public function search($region, $suburb = null) { } 

thanks!

you can specify mask {category} parameter must fit format "category[0-9]+" in order match route.

route::get('/{category}/{region}/{suburb?}', 'searchcontroller@search')     ->where('category', 'category[0-9]+'); 

now, example url (from comments) www.a.com/var1/var2/var3 match route if var1 matches given category regex.

more information can found in documentation route parameters here.

edit

yes, can work array of string values. regex, need put array of string values context:

route::get('/{category}/{region}/{suburb?}', 'searchcontroller@search')     ->where('category', 'hairdresser|cooper|fletcher'); 

or, if have array built somewhere else:

$arr = ['hairdresser', 'cooper', 'fletcher'];  // run each array entry through preg_quote , glue // resulting array pipes route::get('/{category}/{region}/{suburb?}', 'searchcontroller@search')     ->where('category', implode('|', array_map('preg_quote', $arr))); 

edit 2 (solutions original request)

your original question how pass hardcoded category segment controller. if, reason, didn't wish use solution above, have 2 other options.

option 1: don't pass value in, access segments of request in controller.

public function search($region, $suburb = null) {     $category = \request::segment(1);     dd($category); } 

option 2: modify route parameters using before filter (l4) or before middleware (l5).

before filters (and middleware) have access route object, , can use methods on route object modify route parameters. these route parameters passed controller action. route parameters stored associative array, needs kept in mind when trying order correct.

if using laravel 4, you'd need before filter. define routes use before filter , pass in hardcoded value added onto parameters.

route::get('/hairdresser/{region}/{suburb?}', ['before' => 'shiftparameter:hairdresser', 'uses' => 'searchcontroller@search']); route::get('/cooper/{region}/{suburb?}', ['before' => 'shiftparameter:cooper', 'uses' => 'searchcontroller@search']); route::get('/fletcher/{region}/{suburb?}', ['before' => 'shiftparameter:fletcher', 'uses' => 'searchcontroller@search']);  route::filter('shiftparameter', function ($route, $request, $value) {     // save off current route parameters         $parameters = $route->parameters();     // unset current route parameters     foreach($parameters $name => $parameter) {         $route->forgetparameter($name);     }      // union new parameters , old parameters     $parameters = ['customparameter0' => $value] + $parameters;     // loop through new set of parameters add them route     foreach($parameters $name => $parameter) {         $route->setparameter($name, $parameter);     } }); 

if using laravel 5, you'd need define new before middleware. add new class app/http/middleware directory , register in $routemiddleware variable in app/http/kernel.php. logic same, hoop go through in order pass parameters middleware.

// 'parameters' key custom key we're using pass data middleware route::get('/hairdresser/{region}/{suburb?}', ['middleware' => 'shiftparameter', 'parameters' => ['hairdresser'], 'uses' => 'searchcontroller@search']); route::get('/cooper/{region}/{suburb?}', ['middleware' => 'shiftparameter', 'parameters' => ['cooper'], 'uses' => 'searchcontroller@search']); route::get('/fletcher/{region}/{suburb?}', ['middleware' => 'shiftparameter', 'parameters' => ['fletcher'], 'uses' => 'searchcontroller@search']);  // middleware class go in app/http/middleware // generate "php artisan make:middleware" statement , copy logic below class shiftparametermiddleware {     public function handle($request, closure $next) {         // route request         $route = $request->route();          // save off current route parameters         $parameters = $route->parameters();         // unset current route parameters         foreach ($parameters $name => $parameter) {             $route->forgetparameter($name);         }          // build new parameters shift onto array         // data passed middleware         $newparameters = [];         foreach ($this->getparameters($request) $key => $value) {             $newparameters['customparameter' . $key] = $value;         }          // union new parameters , old parameters         $parameters = $newparameters + $parameters;         // loop through new set of parameters add them route         foreach ($parameters $name => $parameter) {             $route->setparameter($name, $parameter);         }          return $next($request);     }      /**      * method data custom 'parameters' key added      * on route definition.      */     protected function getparameters($request) {         $actions = $request->route()->getaction();         return $actions['parameters'];     } } 

now, filter (or middleware) setup , in use, category passed controller method first parameter.

public function search($category, $region, $suburb = null) {     dd($category); } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -