2014-07-16 99 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