我想问一下如何删除路径名中的项目名称。我经历了一个教程,并按照每个练习课程,当我尝试发布这样的注册时:
<form action = "/user/register" method = "post">
<input type = "hidden" name = "_token" value = "<?php echo csrf_token() ?>">
<table>
<tr>
<td>Name</td>
<td><input type = "text" name = "name" /></td>
</tr>
<tr>
<td>Username</td>
<td><input type = "text" name = "username" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type = "text" name = "password" /></td>
</tr>
<tr>
<td colspan = "2" align = "center">
<input type = "submit" value = "Register" />
</td>
</tr>
</table>
</form>
但是当我发布它时,它显示错误,说找不到网址,所以我在网址中添加了项目名称:/test_laravel/user/register
是新操作。它有用,但如何摆脱这个?
谢谢!
答案 0 :(得分:1)
要制作正确的网址,您可以使用:
url
函数生成给定路径的完全限定URL:
$url = route('routeName');
$url = route('routeName', ['id' => 1]);
网址功能会生成一个完整的$ url =路由(&#39; routeName&#39;,[&#39; id&#39; =&gt; 1]); y指定路径的合格网址
echo url('user/profile');
echo url('user/profile', [1]);
action
函数为给定的控制器操作生成URL。您不需要将完整的命名空间传递给控制器。而是相对于App\Http\Controllers
命名空间传递控制器类名:
$url = action('HomeController@getIndex');
$url = action('UserController@profile', ['id' => 1]);
答案 1 :(得分:0)
这就是设计路线的原因。
制作路线档案;
Route::post('user/registration',['uses' => 'ControllerName@MethodName','as' => 'giveRouteAUniqueName']);
uses accepts controller name and after @ accepts method(function) of the controller
and as gives you ability to assign new unique name to route now you can call
route('RouteName'); in our case its route('giveRouteAUniqueName')