I am getting this error:
Facade\Ignition\Exceptions\ViewException: Route [/api/auth/signin] not
defined. (View: C:\wamp\www\Laravel\FormsDashboard\resources\views\login.blade.php) in
file C:\wamp\www\Laravel\FormsDashboard\vendor\laravel\framework\src\Illuminate\Routing\UrlGenerator.php
on line 444
The route http://127.0.0.1:8000/api/auth/signin
is actually directly accessible, but when calling that route from the view it gives that error.
I tried defining the route as
Route::post('signin','AuthController@login')->name('signin');
but that also gives me the same error.
This is my routes/api.php:
Route::group([
'namespace'=>'App\Http\Controllers',
'prefix'=>'auth'
],function(){
Route::post('signin','AuthController@login');
}
This is the view:
<form method="POST">
<button type="submit"
onclick="window.location='{{route('/api/auth/signin')}}'">Submit</button>
</form>
By giving the route as
Route::prefix('auth')->group(function (){
Route::post('signin', 'AuthController@login')->name('signin');
});
I fixed the above error, but upon clicking the submit button on that view page I am getting the error:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The POST method is not supported for this route. Supported methods: GET, HEAD.
I cleared the cache also.
Can anyone tell me what is wrong?
CodePudding user response:
It seems you are using wrong file for routes. if you are above laravel 7 version, you should have "api.php" for api routes.
Put your Routes in api.php if you want to use /api/ in your route path.
Also, in window.location , use
{{ route('signin') }}
or
@json(route('signin'))
I have noticed that you dont have closing ) and ; in the end.
CodePudding user response:
Once you have assigned a name to a given route, you may use the route's name when generating URLs or redirects via Laravel's route helper functions. You haven't declared a name for the url /api/auth/signin
but used this url as a name in route()
method, you can't use a url as a name in route function, to use route()
function declare a name for the url.
Route::group([
'namespace'=>'App\Http\Controllers',
'prefix'=>'auth'
],function(){
Route::post('signin','AuthController@login')->name('auth.signin');
}
CodePudding user response:
The problem is that you didn't declare the route name.
Route::group(['namespace'=>'App\Http\Controllers', 'prefix'=>'auth'],function() {
Route::post('signin', 'AuthController@login')->name('signin');
});
give a id to that particular button then write an ajax call.
$(document).on('click',"#submit",function{
$.ajax({
type: "POST",
url: "{{route('api/auth/signin') }}",
data :{
_token: "{{csrf_token()}}"
}
success : function(){
}
})