【发布时间】:2014-05-21 23:03:11
【问题描述】:
我正在使用 Laravel 框架和刀片模板引擎。
我想做的是,在我的视图中有 2 个按钮,点击后会将您重定向到另一个视图。我试过的代码是:
<button type="button" onclick="{{ Redirect::to('users.index') }}">Button</button>
【问题讨论】:
我正在使用 Laravel 框架和刀片模板引擎。
我想做的是,在我的视图中有 2 个按钮,点击后会将您重定向到另一个视图。我试过的代码是:
<button type="button" onclick="{{ Redirect::to('users.index') }}">Button</button>
【问题讨论】:
你可以试试这个(假设这个代码在刀片模板中):
<button type="button" onclick="window.location='{{ url("users/index") }}'">Button</button>
但是,{{ url('users/index') }} 会打印出url,所以它会在浏览器中变成这样:
<button type="button" onclick="window.location='http://domain.com/users/index'">Button</button>
这意味着,你有一个声明如下的路由:
Route::get('users/index', array('uses' => 'UserController@index', 'as' => 'users.index'));
在这种情况下,也可以使用:
<button type="button" onclick="window.location='{{ route("users.index") }}'">Button</button>
输出将是相同的。
【讨论】:
是的,您基本上需要使用 URL 帮助程序来生成 URL(就像放置 http://yoursite.com/users 之类的东西而不是 PHP 帮助程序一样):
<button type="button" onclick="window.location='{{ URL::route('users.index'); }}'">Button</button>
虽然我不明白为什么你不能只使用“a href”标签而不是按钮,就像这样:
<a href="{{ URL::route('users.index'); }}">My button</a>
【讨论】:
您还可以将 href 标记设置为按钮的样式。例如
<a class="btn btn-primary" href="{{ route('users.create' }}">Create</a>
这有助于避免混合 javascript 和刀片指令。```
【讨论】: