Laravel routes not working as expected

戏子无情 提交于 2021-01-29 07:26:00

问题


I have just created UserController and added a route in the routes.php file:

Route::resource('users', 'UserController');

When I visit laravel.dev/users I expected to see the user index view, but instead I get a 404 error: The requested URL /users was not found on this server.

Here is the output when running: php artisan routes

+--------+------------------------+---------------+------------------------+----------------+---------------+
| Domain | URI                    | Name          | Action                 | Before Filters | After Filters |
+--------+------------------------+---------------+------------------------+----------------+---------------+
|        | GET users              | users.index   | UserController@index   |                |               |
|        | GET users/create       | users.create  | UserController@create  |                |               |
|        | POST users             | users.store   | UserController@store   |                |               |
|        | GET users/{users}      | users.show    | UserController@show    |                |               |
|        | GET users/{users}/edit | users.edit    | UserController@edit    |                |               |
|        | PUT users/{users}      | users.update  | UserController@update  |                |               |
|        | PATCH users/{users}    |               | UserController@update  |                |               |
|        | DELETE users/{users}   | users.destroy | UserController@destroy |                |               |
|        | GET /                  |               | Closure                |                |               |
+--------+------------------------+---------------+------------------------+----------------+---------------+

What have I done wrong?


回答1:


You still need a matching method in the User controller for that route (matches the "action"), if you don't have one:

public function index() {
  return "Hello world!";
}



回答2:


I think there must be some other routes.php file from where your application is running. Can you check it in your application?




回答3:


Assuming that your UserController is error free,

1) Make sure that the url property is configured correctly in app/config/app.php

2) Check whether your .htaccess file is working correctly.

3) Make a composer dump-autoload --optimize



来源:https://stackoverflow.com/questions/21996354/laravel-routes-not-working-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!