我是Laravel的新手,我遇到了路线类
的一些问题这是我的 routes.php
Route::get('/', function () {
return view('welcome');
});
Route::auth();
Route::get('/home', 'HomeController@index');
Route::resource('/dashboard', 'DashboardController');
Route::resource('/dashboard/users', 'DashboardUsersController');
Route::resource('/dashboard/posts', 'DashboardPostsController');
除
外,所有上述路线均有效app.dev/dashboard/users/index
app.dev/dashboard/posts/index
我创建的每条路线
Route::get('dashboard/users/import', function (){
return 'Importing a user';
});
或
Route::get('dashboard/users', 'DashboardUsersController@getUsers');
我已经尝试了一些我在这里找到的建议,比如清理缓存,查看和路由但没有工作,我的项目文件夹的权限是777
这是我的 DashboardUsersController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class DashboardUsersController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('admin.users.index');
}
public function getUsers()
{
return view('admin.users.index');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.users.index');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
return view('admin.users.index');
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
编辑php artisan route:list
+--------+-----------+------------------------------+-------------------------+-----------------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+------------------------------+-------------------------+-----------------------------------------------------------------+------------+
| | GET|HEAD | / | | Closure | web |
| | POST | dashboard | dashboard.store | App\Http\Controllers\DashboardController@store | web |
| | GET|HEAD | dashboard | dashboard.index | App\Http\Controllers\DashboardController@index | web |
| | GET|HEAD | dashboard/create | dashboard.create | App\Http\Controllers\DashboardController@create | web |
| | POST | dashboard/posts | dashboard.posts.store | App\Http\Controllers\DashboardPostsController@store | web |
| | GET|HEAD | dashboard/posts | dashboard.posts.index | App\Http\Controllers\DashboardPostsController@index | web |
| | GET|HEAD | dashboard/posts/create | dashboard.posts.create | App\Http\Controllers\DashboardPostsController@create | web |
| | PUT|PATCH | dashboard/posts/{posts} | dashboard.posts.update | App\Http\Controllers\DashboardPostsController@update | web |
| | DELETE | dashboard/posts/{posts} | dashboard.posts.destroy | App\Http\Controllers\DashboardPostsController@destroy | web |
| | GET|HEAD | dashboard/posts/{posts} | dashboard.posts.show | App\Http\Controllers\DashboardPostsController@show | web |
| | GET|HEAD | dashboard/posts/{posts}/edit | dashboard.posts.edit | App\Http\Controllers\DashboardPostsController@edit | web |
| | GET|HEAD | dashboard/users | dashboard.users.index | App\Http\Controllers\DashboardUsersController@getUsers | web |
| | POST | dashboard/users | dashboard.users.store | App\Http\Controllers\DashboardUsersController@store | web |
| | GET|HEAD | dashboard/users/create | dashboard.users.create | App\Http\Controllers\DashboardUsersController@create | web |
| | DELETE | dashboard/users/{users} | dashboard.users.destroy | App\Http\Controllers\DashboardUsersController@destroy | web |
| | GET|HEAD | dashboard/users/{users} | dashboard.users.show | App\Http\Controllers\DashboardUsersController@show | web |
| | PUT|PATCH | dashboard/users/{users} | dashboard.users.update | App\Http\Controllers\DashboardUsersController@update | web |
| | GET|HEAD | dashboard/users/{users}/edit | dashboard.users.edit | App\Http\Controllers\DashboardUsersController@edit | web |
| | PUT|PATCH | dashboard/{dashboard} | dashboard.update | App\Http\Controllers\DashboardController@update | web |
| | DELETE | dashboard/{dashboard} | dashboard.destroy | App\Http\Controllers\DashboardController@destroy | web |
| | GET|HEAD | dashboard/{dashboard} | dashboard.show | App\Http\Controllers\DashboardController@show | web |
| | GET|HEAD | dashboard/{dashboard}/edit | dashboard.edit | App\Http\Controllers\DashboardController@edit | web |
| | GET|HEAD | home | | App\Http\Controllers\HomeController@index | web,auth |
| | GET|HEAD | login | | App\Http\Controllers\Auth\AuthController@showLoginForm | web,guest |
| | POST | login | | App\Http\Controllers\Auth\AuthController@login | web,guest |
| | GET|HEAD | logout | | App\Http\Controllers\Auth\AuthController@logout | web |
| | POST | password/email | | App\Http\Controllers\Auth\PasswordController@sendResetLinkEmail | web,guest |
| | POST | password/reset | | App\Http\Controllers\Auth\PasswordController@reset | web,guest |
| | GET|HEAD | password/reset/{token?} | | App\Http\Controllers\Auth\PasswordController@showResetForm | web,guest |
| | POST | register | | App\Http\Controllers\Auth\AuthController@register | web,guest |
| | GET|HEAD | register | | App\Http\Controllers\Auth\AuthController@showRegistrationForm | web,guest |
+--------+-----------+------------------------------+-------------------------+-----------------------------------------------------------------+------------+
就像我说的所有上述路线都在工作,除了
/dashboard/users
/dashboard/posts
应该路由到DashboardUsersController和DashboardPostsController的index
方法,以及我创建的每个自定义路由。
当我删除Route::resource
Route::get('dashboard/users', 'DashboardUsersController@index');
工作时,可能是一个错误?还是我忘记设置的东西?
修改2
当我尝试
时Route::get('dashboard/users', 'DashboardUsersController@getUsers');
我删除了
Route::resource('/dashboard/users', 'DashboardUsersController');
确保它不会发生冲突,但仍无法正常工作
答案 0 :(得分:0)
为简单起见,对于控制器中的常规方法使用Route::get
,对于从表单接收Route::post
数据的方法使用$_POST
。
Route::get
/ Route::post
方法的第二个参数是ControllerName @ MethodName。尝试相应地改变它。
尽量不要忘记命名空间。
修改强>
使用Route::resource
时,实际上是为控制器生成所有可能的RESTful路由。你不能不止一次。这就是为什么将Route::resource
更改为Route::get
的原因。
您可以在此处找到更多信息:https://laravel.com/docs/5.2/controllers#restful-resource-controllers
答案 1 :(得分:0)
你并不是很清楚什么在起作用,什么不起作用。您还需要添加更多代码以用于说明目的,并且我们必须填写关于您正在做什么的假设中的空白。
定义资源时,例如Route::resource('/users', 'UserController');
您获得以下路线:
| | GET|HEAD | users | users.index | App\Http\Controllers\UserController@index | web |
| | POST | users | users.store | App\Http\Controllers\UserController@store | web |
| | GET|HEAD | users/create | users.create | App\Http\Controllers\UserController@create | web |
| | GET|HEAD | users/{users} | users.show | App\Http\Controllers\UserController@show | web |
| | PUT|PATCH | users/{users} | users.update | App\Http\Controllers\UserController@update | web |
| | DELETE | users/{users} | users.destroy | App\Http\Controllers\UserController@destroy | web |
| | GET|HEAD | users/{users}/edit | users.edit | App\Http\Controllers\UserController@edit | web |
如果您创建另一条路线Route::get('users', 'UserController@getUsers');
,它将与您的resource
路线定义的第一条路线发生冲突。
所以你需要确保你没有相互冲突的路线。