php 从按钮单击 Laravel 调用路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38709886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call route from button click laravel
提问by rimonmostafiz
I am using the Laravel framework and the blade templating engine for one of my projects, where I have a route which looks like
我在我的一个项目中使用 Laravel 框架和刀片模板引擎,在那里我有一条看起来像
Route::get('/problems/{problem-id}/edit', 'AdminController@editProblem');
I have editProblemmethod in AdminControllerwhich returns a view
我在AdminController 中有editProblem方法,它返回一个视图
public function editProblem(Problem $problem) {
return view('admin.problem-edit', compact('problem'));
}
and I have a button on a view which looks like
我在视图上有一个按钮,看起来像
<button class="btn btn-xs btn-info pull-right">Edit</button>
Now I want to call this route with the $problem->id
when the button will be clicked. I need to pass these value on the route.
现在我想$problem->id
用点击按钮的时间来调用这条路线。我需要在路线上传递这些值。
how can I do that?
我怎样才能做到这一点?
回答by Geraldo Novais
In my opnion, you should use url() Laravel method
在我看来,你应该使用 url() Laravel 方法
To call you route with the problem's id you can do:
要使用问题的 id 给您打电话,您可以执行以下操作:
<a href="{{ url('/problems/' . $problem->id . '/edit') }}" class="btn btn-xs btn-info pull-right">Edit</a>
I used an anchor tag, but it will be rendered like you button tag because I kept the same style class you have defined.
我使用了一个锚标签,但它会像你的按钮标签一样呈现,因为我保留了你定义的相同样式类。
Why you should use url() method ?
为什么要使用 url() 方法?
The reason is simple, the url method will get the full url to your controller. If you don't use this, href link will get appended with current url.
原因很简单,url 方法将获取到您的控制器的完整 url。如果您不使用它,href 链接将附加当前 url。
For example, supose you button is located inside a given page
例如,假设您的按钮位于给定页面内
yourdomain.com/a-given-page/
yourdomain.com/a-given-page/
when someone click in your button, the result will be:
当有人点击你的按钮时,结果将是:
yourdomain.com/a-given-page/problems/{problem-id}/edit
yourdomain.com/a-given-page/problems/{problem-id}/edit
when you would like to get this:
当你想得到这个时:
yourdomain.com/problems/{problem-id}/edit
yourdomain.com/problems/{problem-id}/edit
Some considerations about your editProblem method
关于您的 editProblem 方法的一些注意事项
Your route has the '$id', so you need to receive this '$id' in your method
您的路线具有“$id”,因此您需要在方法中接收此“$id”
public function editProblem($problem_id) {
$problem = \App\Problem::find($problem_id); //If you have your model 'Problem' located in your App folder
return view('admin.problem-edit', compact('problem'));
}
回答by Vipertecpro
Try This:
尝试这个:
<button type="button" onclick="window.location='{{ url("users/index") }}'">Button</button>
Little Suggestion:When you're defining routes in laravel give it a unique name, it helps you to keep track on each url like this
小建议:当你在 laravel 中定义路由时,给它一个唯一的名字,它可以帮助你像这样跟踪每个 url
Route::get('/problems/{problem-id}/edit', 'AdminController@editProblem')->name('pEdit');
Route::post('/problems/{problem-id}/edit', 'AdminController@editProblem')->name('pEdit');
Now you use this route in blade with just name for post and get both
现在你在刀片中使用这条路线,只有名字作为帖子并获得两者
<button type="button" onclick="window.location='{{ route("pEdit",array("parameter1")) }}'">Button</button>
回答by Luis Dalmolin
You will need to create a link to this route:
您需要创建指向此路线的链接:
<a href="/problems/{{ $problem->id }}/edit" class="btn btn-xs btn-info pull-right">Edit</a>
If you use named routes, this will be even easier:
如果您使用命名路由,这将更容易:
Route::get('/problems/{problem-id}/edit', ['as' => 'problems.edit', 'uses' => 'AdminController@editProblem']);
And then you just need to call the route
method:
然后你只需要调用该route
方法:
<a href="{{ route('problems.edit', $problem->id) }}" class="btn btn-xs btn-info pull-right">Edit</a>