php Laravel 路由将变量传递给控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26844484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:04:50  来源:igfitidea点击:

Laravel route pass variable to controller

phplaravellaravel-4laravel-routing

提问by imperium2335

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:

我希望简单地对某些路线的 type_id 进行硬编码(由我固定),如下所示:

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

My ProductsController for reference:

我的 ProductsController 供参考:

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

}

回答by lukasgeiter

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

但是,更好的方法是使用where条件,然后在控制器中进行类型到 id 的转换。但是,您将丢失直接别名,并且在生成 URL 时必须将产品作为参数传递。

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

回答by Ronser

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

希望这可以帮助...

回答by Joe

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 Sluggableto generate a unique slug for each product, then create the following route:

不过,为了制作 SEO 友好的名称,您可以使用Sluggable为每个产品生成一个唯一的 slug,然后创建以下路线:

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

回答by Raheel Hasan

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):

现在在控制器里面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'];
}