Reputation: 75
I'm currently writing an API in Laravel, and I've hit a bit of a snag. At the moment I'm focussed on the Authentication part of my api, so I have the following routes:
(Via php artisan route:list
)
+--------+--------+------------------------+------+--------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+--------+------------------------+------+--------------------------------------------------------+------------+
| | POST | v1/users/auth/login | | App\Http\Controllers\User\AuthController@login | api |
| | POST | v1/users/auth/register | | App\Http\Controllers\User\AuthController@register | api |
+--------+--------+------------------------+------+--------------------------------------------------------+------------+
Very simple. Now, when I try to post to v1/users/auth/login
through Postman, it works. It gives me the expected responses and everything. However, when I try the register
route, Laravel throws up a 404?
No errors, no nothing, just a 404. Now through some caveman debugging I can see that it passes through the authorize()
method of the request, but then it throws that darn error again.
This is my request:
class RegistrationRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return !Auth::check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'code' => 'required',
'username' => 'required|unique:users',
'password' => 'required|confirmed|min:6',
];
}
public function persist()
{
$user = User::create([
'username' => $this->username,
'password' => password_hash($this->password, PASSWORD_DEFAULT),
'reg_ip' => $this->ip(),
'last_ip' => $this->ip()
]);
return response()->json($user->api_token, 201);
}
How how is that possible, as my route is cleary defined?
Thanks.
-- EDIT:
my controller:
class AuthController extends Controller
{
public function register(RegistrationRequest $request)
{
return $request->persist();
}
public function login(LoginRequest $request)
{
return $request->persist();
}
}
Upvotes: 1
Views: 1539
Reputation: 11
To call api routes in laravel add api/ as prefix to the route or else it will search for routes in web.php
Upvotes: 1
Reputation: 1118
add prefix on this route
v1/users/auth/login
use like this
api/v1/users/auth/login
Upvotes: 0