Home > Net >  Route not defined for view
Route not defined for view

Time:03-21

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

Am getting the above error for below code. The route http://127.0.0.1:8000/api/auth/signin is actually directly accessible, but when calling that route from view it gives this error.I also tried defining the route as Route::post('signin','AuthController@login')->name('signin'); .But that also giving the same error. I've cleared cache also.Can anyone tell me what is wrong here?Any help is much appreciated.

routes/api.php

Route::group([
    'namespace'=>'App\Http\Controllers',
    'prefix'=>'auth'   
],function(){
    Route::post('signin','AuthController@login');
 }

view

<form  method="POST">
   <button type="submit"  
 onclick="window.location='{{route('/api/auth/signin')}}'">Submit</button>
</form>

EDIT

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 now am getting the error Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException The POST method is not supported for this route. Supported methods: GET, HEAD.

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(){

                   }
    })
  • Related