简体   繁体   中英

Laravel route pass variable to controller

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));
    }

}

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)');

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...

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
    }
});

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'];
}

Sorry you're all wrong...

Route::GET('/url-name', function(Request $request){

    // Pass in custom SEO DATA
    $seo = [
        'title' => 'Hello world',
        'description' => 'some Description'

    ];

     $c = new \App\Http\Controllers\MyController();
     return $c->index($request, $seo);
});

In the controller....

public function index(Request $request, $seo = null)
    {

        dd($seo);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM