I'm using laravel 7.x.
since function(){return view(welcome);} will produce an error when I run php artisan route:cache, so I write this code below to replace function():
Route::get('/', 'WelcomeController#index')->name('welcome');
It run well on php artisan serve command.
But when i run directly from public folder, it produce exception MethodNotAllowedHttpException. I couldn't find why it happened, could you help me why it happened?
exception message:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: HEAD.
https://localhost/laravel-tester/public/
WelcomeController Method
public function index(){
return view('welcome');
}
::get registers the route for methods GET and HEAD by default. You are trying to access it with a GET request (as you should) but it does not return it. Possibly there is something wrong with your Router class, so please check your Routing\Router.php class against the method in the comment below.
https://stackoverflow.com/a/22119028/10187949
Related
I just installed Laravel 8 and created new controller and route. when i try to use new route that i created which is working fine but route('/') is not working. giving me error
The GET method is not supported for this route. Supported methods: HEAD.
route/web.php
Route::get('/', function () {
return view('welcome');
});
Route::get('login', [LoginController::class,'loginShow'])->name('login');
LoginController
class LoginController extends Controller
{
function loginShow(){
return view('Login.login');
}
}
Route List
Problem Detail :
here i have 2 routes 1) mydomin or mydomin/ 2) myDomian/login
here myDomian/login is working as i want but when i try to use mydomin then i am getting
many time i face thing problem but sometime i fix anyway. But i want to know its real reason why it happen? so next time i will take case self. here mydomin is GET method route and i delcleared in web.php as GET. so why it is telling it is HEAD not get? Also note that before create a new route it was working in same way. so why now not? please tell me reason of this.
i got my solution using php artisan serve . so after that it is start working. but i a could not understand why it is not working without php artisan serve .... if any one has any reason please tell us.
If we just create a 404.blade.php page in resources/views/error it will work fine, but Auth() won't work on 404 page, to solve that if we follow the solution available on stackoverflow the Laravel auth errors will stop working. I use the following solution to do the work.
Create custom view
resources/views/errors/404.blade.php
in route.php
Route::any('{catchall}', 'PageController#notfound')->where('catchall', '.*');
create PageController and add this function
public function notfound()
{
return view('errors.404');
}
For Laravel 5.6 and later, you can use fallback in your routes\web.php:
Route::fallback('MyController#show404');
It works as an "catch all"-route.
See docs here.
Write below code in your Exceptions/Handler.php
if($this->isHttpException($exception)){
if(view()->exists('errors.'.$exception->getStatusCode())){
$code = array('status'=>$exception->getStatusCode());
return response()->view('errors.404',compact('code'));
}
}
Now create a new file in your view i.e errors/404;
Now in code array you can pass dynamic values
Not sure if this will help. Laravel has a PHP artisan command to publish error pages. Laravel Custom HTTP Error Pages
After you run this artisan command use Route:fallback() method as #KFoobar suggested. If you use a closure function, no need to use a controller. Make sure to add the below route at the ends of your routes file.
//Fallback/Catchall Route
Route::fallback(function () {
return view('errors.layout');
});
Shameless plug for my BLOG
My api.php route is
Route::get('/allposts','PostController#index');
Controller function is and working with web.php Route file
public function index()
{
$posts= Post::all();
return PostResource::collection($posts);
}
my Resource is toArray function is
public function toArray($request)
{
return parent::toArray($request);
}
Postman link using GET are
"queuetest.com/api/allposts"
"http://queuetest.com/api/allposts"
both are not working
and getting Result in both Post man and browser: Sorry the page your looking for could not be found
Do you get something other than the Sorry the page you're looking for could not be found message when you do var_dump('test');die(); in your index method as the first line.
Edit: try to remove then / before /allPosts
Another edit: check in your RouteServiceProvider if the mapApiRoutes prefix is set to 'api'
If you are using the default route configuration that comes out of the box, you should place your route in routes/api.php if you want to access it in the /api namespace. Right now, the route should be accessible from http://queuetest.com/allposts.
This behavior can be configured in App\Providers\RouteServiceProviders.php. If you have a modified route configuration, there are numerous things that could cause this behaviour, and it is impossible to locate the problem without seeing more code.
I have RestaurantController with these methods:
save
show($id)
when I finish executing the save method, I want to redirect the user to the show($id) method.
I tried this:
return Redirect::route('show', array($restaurant->id));
but I got :
InvalidArgumentException
Route [/show] not defined.
I also tried this:
NotFoundHttpException
though the show method exists.
in my routes.php I have:
Route::resource('restaurants', 'RestaurantsController');
could you help please?
Route is the name of the route, so in this instance it'd be:
return Redirect::route('restaurants.show', [$restaurant->id]);
See here for more information regarding redirects.
Also, just fyi, from the command line you can run php artisan routes to see a full list of routes and their corresponding names.
I use laravel 4 and am not able to show a component :
this is a line from my routes
Route::resource('/', 'PostsController');
and this is my show function from PostsController.php
public function show($id) {
return "HI";
}
And This is the line that links to the function from my view
<h1>{{$post['title']}}</h1>
And It properly links to localhost:8000/show/1
But I'm amazingly getting a Not found HTTP exception from laravel.
How do I get this to work?
So the answer is that #NihalSahu is wrong (me) and that resourceful routing doesn't work like that it would be infinitely better so as to set the router to host/{id}