Laravel route pass variable to controller

46,845

Solution 1

You can use a closure for your route and then call the controller action:

Route::get('/milk', array('as' => 'milk', function(){
    return App::make('ProductsController')->index(1);
}));

However, a nicer way would be to use a where condition and then do the type-to-id conversion in the controller. You will lose the direct alias though and would have to pass in the product as parameter when generating the URL.

Route::get('{product}', array('as' => 'product', 'uses' => 'ProductsController@index'))
    ->where('product', '(milk|cheese)');

Solution 2

I have used this to pass values to the controller...

route:

Route::get('user/{user}/usermanage',  array('as' => 'userdata.usermanage',       'uses' => 'yourController@getUserDetails'));
//{user} - holds some value...

in controller:

public function getUserDetails($id)
{
    ...
}

if want dynamic :

$var    =   "Lists"; 

Route::get('something',        array('as' => 'something',      'uses' => 'yourController@get'.$var));

hope this helps...

Solution 3

I feel like the tidiest way to do this is probably with route constraints:

Route::get('{milk}', [ 'as' => 'milk', 'uses' => 'ProductsController@index' ])
     ->where('milk', 'milk'); // matches the named arg {milk} (param 1)
                              // to the regex literal 'milk' (param 2)

It has some redundancy, but if you want to do it purely from your routes, I'd go with this.

For making SEO-friendly names though, you could use Sluggable to generate a unique slug for each product, then create the following route:

Route::get('{product}', [ 'as' => 'product', 'before' => 'product-slug', 'uses' => 'ProductsController@index' ])
     ->where('product', '[a-z0-9]+[a-z0-9\-]*'); // valid slug syntax

And this filter:

Route::filter('product-slug', function($route) {
    $slug = $route->getParameter( 'slug' );
    if (is_numeric($slug)) { // if the slug is an ID
        $product = Product::findOrFail($slug); // try to find the product
        return Redirect::route('product', $product->slug); // and redirect to it
    }
});

Solution 4

Here is how you actually do it without messing up the url:

Define Route:

Route::match(['GET', 'POST'], 'my-url', ['var_1'=>'hello', 'var_2'=>'world', 'prefix'=>'my-prefix', 'middleware'=>['web', 'mid2', 'mid3'], 'as'=>"my-route-name", 'uses'=>'myController@index']);

Now in the controller, inside function __construct(Request $request):

$req_action = @$request->route()->getAction();

$var_1 = $var_2 = '';
if(is_array($req_action) && !empty($req_action['var_1'])){
$var_1 = (int)@$req_action['var_1'];
}

if(is_array($req_action) && !empty($req_action['var_2'])){
$var_2 = @$req_action['var_2'];
}
Share:
46,845

Related videos on Youtube

imperium2335
Author by

imperium2335

PHP, JS, MySQL coder and retired 3D modeler.

Updated on July 09, 2022

Comments

  • imperium2335
    imperium2335 almost 2 years

    How do I pass a hard coded variable to a controller?

    My route is:

    Route::group(array('prefix' => $locale), function() {
        Route::get('/milk', array('as' => 'milk', 'uses' => 'ProductsController@index'));
    });
    

    I want to do something like:

    Route::get('/milk', array('as' => 'milk', 'uses' => 'ProductsController@index(1)'));
    

    But that doesn't work.

    How can this be done?


    Sorry if I have not explained well.

    I wish to simply hardcode (set in stone by me) the type_id for certain routes like so:

    Route::get('/milk', array('as' => 'milk', 'uses' => 'ProductsController@index(1)'));
    Route::get('/cheese', array('as' => 'cheese', 'uses' => 'ProductsController@index(2)'));
    ...
    

    My ProductsController for reference:

    class ProductsController extends BaseController {
    
        public function index($type_id) {
            $Products = new Products;
            $products = $Products->where('type_id', $type_id)->get();
            return View::make('products.products', array('products' => $products));
        }
    
    }
    
    • imperium2335
      imperium2335 over 9 years
      @Jerodev It is hard coded into the routes. e.g. milk is 1, orange juice is 2, bicuits is 3 etc. This is so I can have SEO friendly names really otherwise it would be easy i.e. Route::get('/product/{id}.....
  • prograhammer
    prograhammer over 8 years
    Works for Lumen as well. Nice!

Related