繁体   English   中英

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

[英]Laravel route pass variable to controller

如何将硬编码变量传递给控制器​​?

我的路线是:

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

我想做类似的事情:

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

但这不起作用。

如何才能做到这一点?


对不起,如果我没有很好地解释。

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

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

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

}

您可以为路由使用闭包,然后调用控制器操作:

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

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

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

我用它来将值传递给控制器​​......

路线:

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

在控制器中:

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

如果想要动态:

$var    =   "Lists"; 

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

希望这可以帮助...

我觉得最整洁的方法可能是使用路线限制

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)

它有一些冗余,但如果你想纯粹从你的路线上做到这一点,我会选择这个。

不过,为了制作 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

这个过滤器:

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

这是您在不弄乱网址的情况下实际执行的操作:

定义路线:

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

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

对不起你们都错了...

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

在控制器中......

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

        dd($seo);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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