2014-07-16 98 views
0

這是我如何我打電話它在同一個控制器的create.blade.php作爲路由我試圖撥打:傳遞變量與URL路徑::在Laravel

{{ Form::open(['route' => 'myRoute']) }} 
    <button type="submit" href="{{ URL::to('myRoute') }}" class="btn btn-danger btn-mini">Delete</button> 
{{ Form::close() }} 

的路線是:

Route::post('myRoute', ['as' => 'timeline.myRoute', 'uses' => '[email protected]']); 

我想一個整數傳遞到路線。我知道 - > with()在View :: make()中不起作用。什麼是將變量傳遞到myRoute的有效方法?任何幫助表示讚賞。

回答

1

Jeemusu爲90%正確但忘記了在打開表單時指定一個變量。

什麼最終結束了工作是:

{{ Form::open(array('route' => array('timeline.myRoute', $id))) }} 
     <button type="submit" href="{{ URL::route('timeline.myRoute', array($id))  }}" class="btn btn-danger btn-mini">Delete</button> 
{{ Form::close() }} 

與路線在Route.php:

Route::post('myRoute/{id}', ['as' => 'timeline.myRoute', 'uses' => '[email protected]']); 

而且在我的控制器功能:

class TimelineController extends BaseController 
{ 
    public function myRoute($id) { 
      return $id; 
    } 
} 

希望這有助於任何有我的問題的人。

1

您可以使用route parameters將數據傳遞到從URL控制器。

假設您有一個網址像http://yoursite.com/myRoute/id_number_here。你的路線和控制器可能看起來像這樣。

路線

Route::post('myRoute/{id}', ['as' => 'timeline.myRoute', 'uses' => '[email protected]']); 

控制器

public function myRoute($id) { 
    return $id; 
} 
+0

我該如何在我的按鈕中調用它?我試圖'HREF = 「{{URL ::路線( 'timeline.deleteItem',陣列( 'ID'=> 1))}}」'但頁面只是返回'{ID}' – user2480176

+0

的URL定向到是還通過'http://本地主機:8000/myRoute /%7Bid%7D'我不明白 – user2480176

0

嘗試使用URL::route('timeline.myRoute', array(1));甚至URL::to('myRoute', array(1));

編輯:

您路線是這樣的:

Route::get('myRoute/{id}', ['as' => 'timeline.myRoute', 'uses' => '[email protected]']); 

然後當你調用它與此:

echo URL::route('timeline.myRoute', array($id)); 

你可以在你的控制器訪問:

class TimelineController extends BaseController 
{ 
    public function myRoute($id) { 
     echo $id; 
    } 
} 
+0

那麼如何我訪問'公共職能myRoute()'整數? – user2480176

+0

我編輯了我的答案以提供更好的示例。 –

+0

它重定向到http://本地主機:8000/myRoute /%7Bid%7D與錯誤'的Symfony \元器件\ HttpKernel \異常\ MethodNotAllowedHttpException' – user2480176