Laravel 路由错误:路由 [登录] 未定义

Posted

技术标签:

【中文标题】Laravel 路由错误:路由 [登录] 未定义【英文标题】:Laravel route Error: Route [login] is not definded 【发布时间】:2019-01-15 00:50:18 【问题描述】:

我正在开发一个 Laravel API,发现以下问题的正确答案有点困难。问题是我在查询我的组/加入端点时遇到了以下错误。

InvalidArgumentException: Route [login] not defined. in file\vendor\laravel\framework\src\Illuminate\Routing\UrlGenerator.php on line 372

当我查询我的 GroupController@getAll 时,我正在从数据库中获取结果。所以现在我对接下来要尝试什么有点慌张。

我的组控制器代码(仅实现的请求)

/**
 * @param CreateGroupRequest $request
 */
public function createGroup(CreateGroupRequest $request)
    $data = $request->get('group', []);


/*
 * @param JoinGroupUserRequest $request
*/
public function joinGroup(JoinGroupUserRequest $request)
    $group_id = $request->group_id;
    $user_id = $request->user_id;

    $data = $this->groupService->joinGroup($group_id, $user_id);

    return response()->json([], 201);

我的组存储库

class GroupRepository

/*
    Get a new instance of empty Group Model
*/
public function GetModel()
    return new Group();


/**
 * @param int $private
 * @return mixed
 */
public function GetAllGroups($private = 0)
    $groups = Group::where('group_private', $private)->get();

    return $groups;


/**
 * @param array $data
 * @return Group
 */
public function CreateGroup(array $data)
    $group = $this->GetModel();
    $group->fill($data);
    $group->save();

    return $group;


public function AddUserToGroup(int $group_id, int $user_id)
    $group_user = new GroupUsers();
    $group_user->group_id = $group_id;
    $group_user->user_id = $user_id;
    $group_user->save();

    return $group_user;


我的群服务

class GroupService

private $groupRepository;

/**
 * GroupService constructor.
 * @param php $groupRepository
 */
public function __construct(GroupRepository $groupRepository)
    $this->groupRepository = $groupRepository;


/*
 * Gets all Groups
*/
public function getAll()

    return $this->groupRepository->GetAllGroups();


public function joinGroup($group_id, $user_id)
    return $this->groupRepository->AddUserToGroup($group_id, $user_id);


我的 api 路由

Route::group(['middleware' => 'web', 'prefix' => 'auth'], function () 
   Route::post('login', 'AuthController@login');
   Route::post('signup', 'AuthController@signup');
   Route::get('social/provider', 'AuthController@signupSocial');
   Route::get('callback/service', 'AuthController@callback');

   Route::group(['middleware' => 'auth:api'], function() 
      Route::get('logout', 'AuthController@logout');
      Route::get('user', 'AuthController@user');
   );
);

Route::group(['middleware' => 'auth:api', 'prefix' => 'group'], function () 
   Route::get('/', 'GroupController@getAll');
   Route::post('join', 'GroupController@joinGroup');
);

邮递员请求

POST /api/group/join HTTP/1.1
Host: localhost:8000
Content-Type: application/json
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjBhNThiOWY4NTBmZTNmNDZmMTQzYmM2NmY1NzVkZThkMTRiOGFhZjZjMWU5ZThiMjJjNDU3N2QzNmYxYTgyM2JkMjJiMTMzNzBhODcyODAxIn0.eyJhdWQiOiIzIiwianRpIjoiMGE1OGI5Zjg1MGZlM2Y0NmYxNDNiYzY2ZjU3NWRlOGQxNGI4YWFmNmMxZTllOGIyMmM0NTc3ZDM2ZjFhODIzYmQyMmIxMzM3MGE4NzI4MDEiLCJpYXQiOjE1MzM2NjY3NjIsIm5iZiI6MTUzMzY2Njc2MiwiZXhwIjoxNTY1MjAyNzYyLCJzdWIiOiI2Iiwic2NvcGVzIjpbXX0.s96tp1nxYJoXV8j1JNsmPKz0yw0qF1G13v2581HU6uVt5WJkOdXF4ysOQdccIaBDO05CPwqtzjtjgGDV41EuCWgXeT0qYJwPtZzx6OhYmeZiSlsYvC69ttxWRMFIefpX1tEZH0CaFVTV0ZaMpuwBdY7ElDxjM_XWuApFIyouqvNudKrMT0DztY1HrzOeqzzLBZgJbsrrTEnndq37TpXaFBjMfy0GCEt1RFNuGEkws1cQo4SBVt4Zbqdevmyo6kJ2rFMjOn6YdDVg-eYE08X1Qn-51fuHabgKy33_UnwvBATNpF0DgzjmaD7s9C0u8B1T9VIEdRnL6Fr9nVDaIV9aTcSozA-xdLQ7CLNgGLxkilw5Pm4tjo75-UcD-xMdvJ4APWMzk1R4VHa11JjPUzs_4aVLegwE3apExYxjMXO4wC0pyxUoY-1QvVloUEbckx2iJI91P16aKgvKl8IgxjZZdeYVjLwc6IRAHtF4Rv0PXSD6t_1IDSZydiu7s_mT0p3rRxF59bNC23O1QOtdKsYB6Bk1T9mdxG5ndTX_v2HqPZyhjuZQzmOJUH6GotkRPvcWldN-g0kKwA4dF2cYVA7el4RXge_bAAUbbas3l0pWuMNBJEfW78Kh7mmG9oJjj5Qipqzd7clWRhtkUyOikHPvIrJyLdNVdFNyfOeHesjWeaU
Cache-Control: no-cache
Postman-Token: c4b9178e-f64c-4146-ab7f-7453961e11f7


"group_id": "1",
"user_id": "1"
 

现在我尝试的是

清除了我的路由缓存 命名我的登录路径,它给出了 MethodNotAllowedException

有人能把我推向正确的方向吗?

【问题讨论】:

你是如何使用这个的?您需要命名路由,但您还需要一个 POST 请求,因为您没有用于登录的 GET 路由。 我将根据我的邮递员请求进行编辑。 【参考方案1】:

解决方案是使用 application/json 添加 Accept 标头

【讨论】:

以上是关于Laravel 路由错误:路由 [登录] 未定义的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 路由未定义错误

Laravel 5.1 UrlGenerator 路由未定义错误

未找到 Laravel 注册索引并创建错误

Laravel 路由动作未定义

错误-视图中未定义路由-> Laravel

我在 Laravel 中的表单出现“路由未定义错误”