I'm new to laraval. I got this POST route but if someone try the address on the browser it become a GET route and laravel throw a error. Is there a way to throw a 404 page for a GET request on POST route.
Route::post('/step2Validate', 'StepController#step2Validate');
If this route is access as GET "The GET method is not supported for this route. Supported methods: POST."
error is given.
Try this:
Route::get('/step2Validate', function() {
abort(404);
}
);
Related
I would appreciate any help you could give me with the following problem I have.
I am trying to implement a Single Page Application (SPA) with Laravel and Vue, at the moment I am defining the routes, I have something like the following:
routes/app.php
Route::group(['middleware' => 'web'], function () {
Route::any('{path}', function () {
return view('index');
});
});
What I'm trying to do here is the following:
For the middleware web you will only have one view available, in this case in the file index in (resources/views/index.php)
I want that regardless of the method used, Laravel returns the view I want. But I have not succeeded.
Output of php artisan route:list
And when I try to verify with Postman I receive a 404 in response regardless of the method or the URL that I requested .. what could I be doing wrong? Thank you very much in advance.
EDIT 1
I have modified my router and added a conditional as follows:
Route::group(['middleware' => 'web'], function () {
Route::any('{path}', function () {
return view('index');
})->where('path', '.*');
});
Now the GET and HEAD method work properly, however ... when I try the POST method, I get the following error ..
EDIT 2
As mentioned #lagbox in the comments below, error 419 refers to missing CSRF token. I could disable this check in the file Middleware/VerifyCsrfToken.php.
However I wish I could just return the desired view .. without first checking the CSRF token. Another detail that I find is the following:
When in Postman I make a request by a method different from the typical methods ... let's say UNLINK method, I get the following result:
Which leaves me a bit confused, when I define in my routes file web.php Route::any, does it mean any route or not?
I have a route that supports PUT:
Route::put('/products/{id}/cancel/', 'ProductController#cancel')->where('id', '[0-9]+');
After the Controller updates the state of the product it should redirect to another route:
return redirect('products')->with('success', 'Produto cancelado');
that points to
Route::get('/products', 'ProductController#list')->name('products');
All is working until the Redirect.
The product is updated but the redirect gives a method not supported error:
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
The PUT method is not supported for this route. Supported methods: GET, HEAD
I've also tried the following redirects:
return redirect('/products/')->with('success', 'Produto cancelado');
and
return redirect()->route('products')->with('success', 'Produto cancelado');
How is the method PUT passed? Should I explicitly invoke the GET method in the redirect?
How should I solve this?
EDIT:
I'm sending the PUT from a HTTPrequest. So I'm returning to the JS function. I want to forward to a view that accepts the with->.
EDIT2:
I can return a view but it only appears on the network>preview of the Developer Tools.
return view('pages.products')->with('success', 'Produto cancelado');
But I need it in the window obviously.
I could redirect in the js but that way I miss showing the flash messages in the partial blade.
I don't know what to do.
Thanks
The error is not in the GET method. the error says put method is not supported
Route::put('/products/{id}/cancel/', 'ProductController#cancel')->where('id', '[0-9]+');
If you try this as domainname.com/products/5/cancel then define it as below.
Route::get('products/{id}/cancel', 'ProductController#cancel')->where('id', '[0-9]+');
I ended up doing some cheating:
I added a new PUT route:
Route::put('/products', 'ProductController#list')->name('products_put');
the controller returns:
return redirect()->route('products')->with('success', 'Produto cancelado',200);
and the reponse handler does the cheating:
document.write(this.responseText);
window.history.pushState('products', 'eBaw ยท online shopping', '/products');
I am working on an api with laravel. For this, I wanna return a json response for all 404 errors. I created this fallback route and placed it at the end of the api.php:
Route::fallback(function(){
return response()->json(['message' => 'Not Found.'], 404);
})->name('api.fallback.404');
When I now enter an invalid url like /xyz I get the json response as expected.
But when I use route model binding:
Route::get('/projects/{project}', function (\App\Models\Project $project) {
return $project->toArray();
});
and try to get a non existing project (for example /projects/9999), then I get the standard laravel 404 HTML response and not my json one.
How to fix this?
my project is running on Laravel 5.4 and I use passport to make authentication via api with bearer token. everything works fine, but when unauthorized user tries to reach resource that require authentication, the user gets error message 405 method not allowed
but I want response to be 401 unauthorized .
how can change this, and send only response with message, instead of exception? I did research, but couldn't find anything. I use standard laravel middleware for authorization auth:api. my routes grouped in middleware
Route::group(['middleware' => 'auth:api'], function () {
// these routes should return 401 if user not authenticated
}
Well method not allowed exception happens because you are hitting the wrong endpoint. You are posting to a get or vice verca.
However you can modify your exceptions if you go to App\Exception open up handler.php in render() method there you can adjust exceptions as you want example:
public function render($request, Exception $exception)
{
if ($exception instanceof \Illuminate\Auth\AuthenticationException) {
return response('unauthorized', 401);
}
return parent::render($request, $exception);
}
On handler() method just check if $exception is instance of any exception object, if so you can modify the response as you want. For laravel exceptions follow link
I'm using Laravel 4 framework and I've defined a whole bunch of routes, now I wonder for all the undefined urls, how to route them to 404 page?
In Laravel 5.2. Do nothing just create a file name 404.blade.php in the errors folder , it will detect 404 exception automatically.
Undefined routes fires the Symfony\Component\HttpKernel\Exception\NotFoundHttpException exception which you can handle in the app/start/global.php using the App::error() method like this:
/**
* 404 Errors
*/
App::error(function(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $exception, $code)
{
// handle the exception and show view or redirect to a diff route
return View::make('errors.404');
});
The recommended method for handling errors can be found in the Laravel docs:
http://laravel.com/docs/4.2/errors#handling-404-errors
Use the App::missing() function in the start/global.php file in the following manner:
App::missing(function($exception)
{
return Response::view('errors.missing', array(), 404);
});
according to the official documentation
you can just add a file in: resources/views/errors/
called 404.blade.php with the information you want to display on a 404 error.
I've upgraded my laravel 4 codebase to Laravel 5, for anyone who cares:
App::missing(function($exception) {...});
is NO LONGER AVAILABLE in Laravel 5, in order to return the 404 view for all non-existent routes, try put the following in app/Http/Kernel.php:
public function handle($request) {
try {
return parent::handle($request);
}
catch (Exception $e) {
echo \View::make('frontend_pages.page_404');
exit;
// throw $e;
}
}