简体   繁体   中英

Why am i getting an error 404 when i run this code in laravel 9?

I was watching an old tutorial about laravel 7 whiles using laravel 9 , i tried to create a HTML form like this.

                    <div class="card-body">
                    <form action="/upload" method="post" enctype="multipart/form-data">
                        @csrf
                        <input type="file" name="image">
                        <input type="submit" value="upload">
                    </form>   
                  </div>

then in my route(web.php) i added a code like this

route::post('/upload', function(Request $request)
{$request->image->store('images', 'public');
return 'image uploaded succesfully';

but in my webiste it tells me page the url you requested is not found on the site serve

You've defined your route using POST meaning it will only respond to POST requests.

If you try to access /upload from a web browser, that uses a GET request which you've not defined a route for. So you want to define such a route:

Route::get('/upload', function () {
    return view('upload.blade.php');
});

You'll want to replace upload.blade.php with the name of your view that has your upload form in it.

Name Your Route 1st, hit this command php artisan route:clear then try..

Change the form action action="{{ route('upload') }}"

 <div class="card-body">
                    <form action="{{ route('upload') }}" method="post" enctype="multipart/form-data">
                        @csrf
                        <input type="file" name="image">
                        <input type="submit" value="upload">
                    </form>   
                  </div>

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