2014-07-22 193 views
0

使用下面的代碼,我試圖將通配符傳遞給我的控制器,但我不確定如何動態地傳遞URL,我不知道如何在不創建路由的情況下執行此操作每個網址,這將永遠。目前,我試圖做到這一點,像這樣:Laravel的通配符

<a href="{{ route('purchase-get') }}/$item->name"> 

下面是代碼

    <tbody class="text-center"> 
         @foreach (array_chunk($items->all(), 3) as $item_each) 
         <tr> 
          @foreach($item_each as $item) 
          <td> 
           <a href="{{ route('purchase-get') }}/$item->name"> 
           {{ HTML::image($item->image_url, 'item-image', array('class' => 'item-image-row')) }} 
           <h4>{{ $item->item }}</h4> 
           <span class="text-muted">{{ $item->cost }}</span> 
           </a> 
          </td> 
          @endforeach 
         </tr> 
         @endforeach 
        </tbody> 
+2

你可以發佈你的路線文件,請 – Laurence

+0

你真的應該爲每個URL創建一個規則,順便說一句。 – rmobis

回答

0

您可以鏈接到一個通配符名爲路線的其餘部分:

Route::get('/', array('as' => 'index', function() 
{ 
    $slug = 'product-1'; 
    return '<a href="'.URL::route('products', $slug).'">link to product</a>'; 
})); 

然後用通配符剔除名稱路線:

Route::get('products/{slug}', array('as' => 'products', function($slug) 
{ 
    $product = Product::where('slug','=',$slug)->first(); 
    return $product; 
}));