Php 从按钮单击laravel调用路由

Php 从按钮单击laravel调用路由,php,laravel,laravel-5,routes,balde,Php,Laravel,Laravel 5,Routes,Balde,我在我的一个项目中使用了Laravel框架和刀片模板引擎,其中我有一条路径 Route::get('/problems/{problem-id}/edit', 'AdminController@editProblem'); 我在AdminController中有editProblem方法,该方法返回一个视图 public function editProblem(Problem $problem) { return view('admin.problem-edit', comp

我在我的一个项目中使用了Laravel框架和刀片模板引擎,其中我有一条路径

Route::get('/problems/{problem-id}/edit', 'AdminController@editProblem');
我在AdminController中有editProblem方法,该方法返回一个视图

public function editProblem(Problem $problem) {
        return view('admin.problem-edit', compact('problem'));
    }
我在视图上有一个按钮,看起来像

<button class="btn btn-xs btn-info pull-right">Edit</button>
编辑
现在我想在点击按钮时用
$problem->id
调用此路由。我需要在路线上传递这些值


我如何才能做到这一点?

您需要创建指向此路由的链接:

<a href="/problems/{{ $problem->id }}/edit" class="btn btn-xs btn-info pull-right">Edit</a>
然后,您只需调用
route
方法:

<a href="{{ route('problems.edit', $problem->id) }}" class="btn btn-xs btn-info pull-right">Edit</a>

在我的选项中,应该使用url()Laravel方法 要使用问题id调用route,您可以执行以下操作:

<a href="{{ url('/problems/' . $problem->id . '/edit') }}" class="btn btn-xs btn-info pull-right">Edit</a>
试试这个:

<button type="button" onclick="window.location='{{ url("users/index") }}'">Button</button>
现在,您在blade中使用此路径,只需为post命名,即可同时获得这两个名称

<button type="button" onclick="window.location='{{ route("pEdit",array("parameter1")) }}'">Button</button>
按钮

如果我想使用
而不是
,那么我该怎么做?然后你需要添加一些javascript。比如:
,然后用JS捕捉点击并重定向。@mostafiz13在这种情况下,您应该在语义上使用锚定。虽然没有硬性规定,但使用按钮导航用户没有什么意义,因为这正是锚的作用。嗨@HL96,这个“{{”在其他“{”中不起作用。所以我删除了内部的花括号,并将其保留如下:
href=“{{url('/problems/'.$problems->id./edit')}”
<button type="button" onclick="window.location='{{ url("users/index") }}'">Button</button>
Route::get('/problems/{problem-id}/edit', 'AdminController@editProblem')->name('pEdit');
Route::post('/problems/{problem-id}/edit', 'AdminController@editProblem')->name('pEdit');
<button type="button" onclick="window.location='{{ route("pEdit",array("parameter1")) }}'">Button</button>