简体   繁体   中英

Why am i getting “Route [/login] not defined error” in Laravel?

I'm trying to upload a file (.csv) with the help of HTML form (blade file) and a function inside my Maincontroller.php . But as soon as i press the submit button after uploading a file, it displays the Errors "InvalidArgumentException in compiled.php" followed by "Route [/login] not defined."

My uploadform.blade.php file is :

<html>
    <body>
        <form method="post" action="/upload-file" enctype="multipart/form-data">
            <input type="file" name="datafile" />
            <button type="submit">Submit!</button>
        </form>
    </body>
</html>

My uploadFile function (inside MainController.php file) signature is :

public function uploadFile() {

    /* ------------------- */

    /* I WANT TO REACH HERE */

    /* ------------------- */
}

And, in the routes file (ie inside web.php file) is :

Route::post('/upload-file', 'MainController@uploadFile');

On a side note, upon replacing the post keyword with the get keyword (in the blade and route files), the uploadFile function is getting triggered, but it is of no use for me, as file upload (and accessing the csv file data) won't work with GET method.

Route [/login] not defined.

So somewhere (most probably in a blade file) you got that wrong like this route('/login') and should be route('login') .

Do a global search through whole project for /login and fix it. :)

Also check if this route exists by typing php artisan route:list in console/terminal.

| Domain | Method    | URI   | Name  | Action                                                  | Middleware |
|--------|-----------|-------|-------|---------------------------------------------------------|------------|
|        | GET|HEAD  | login | login | App\Http\Controllers\Auth\LoginController@showLoginForm | web,guest  |
|        | POST      | login | login | App\Http\Controllers\Auth\LoginController@login         | web,guest  |

I suggest that for uploading the file your system require user to be logged in. And the framework redirects it to login route. Probably you don't have Auth routes and that is why you get 404. php artisan make:auth will add default routes and views Then add to routes.php Auth::routes(); (if doesn't exists) Check Laravel documentation: https://laravel.com/docs/5.7/authentication#authentication-quickstart

Just got it fixed using the {{csrf_token}} in uploadform.blade.php file. Think its not a very palpable solution, but got it fixed. But, any more explanation regarding this is very much welcome. Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM