簡體   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