Laravel 5.4 route name not working - laravel

In my routes on web.php I have the following line
Route::get('/', 'DashboardController#create')->name('dashboard');
In my DashboardController.php I have a create function with the following line like I saw on a Laracast tutorial but it's not working.
return redirect()->dashboard();
I get the following error
(1/1) FatalThrowableError
Call to undefined method Illuminate\Routing\Redirector::dashboard()
What could I be doing wrong?

You should use
return redirect()->route('dashboard');
this is the way to do it.
visit Named Routes for more info

return redirect()->dashboard(); calls a method named as dashboard inside your controller and that's what error is saying
(1/1) FatalThrowableError
Call to undefined method
Illuminate\Routing\Redirector::dashboard()
You need to call named routes like this
return redirect()->route('dashboard');
For deep insight always trust laravel docs

Instead of:
return redirect()->dashboard();
Try:
return redirect()->route('your-route-name');

Related

Customise the "method is not supported for this route" error handling in Laravel

Route::post('order', 'OrderController#store')->name('order');
When I browse to the URL http://127.0.0.1:8000/order it shows the error:
The GET method is not supported for this route. Supported methods: POST.
Which is the correct.
But I want to redirect user to home page instead of showing this error.
First of all note that what you are trying to do seems like an anti-pattern for Laravel. Accessing a route with the wrong method should be denied!
I currently do not know about altering the default way of handling the wrong method error and I wouldn't advise to do that. But you can work around it:
Patching
Method 1
Keep your routes file clean but alter the original route line and add some lines to the beggining of the controller method
Route::match(['get', 'post'], 'order', [OrderController::class, 'store'])->name('order');
public function store(Request $request)
{
if ($request->isMethod('get')) {
return to_route('home');
}
// ...
Method 2
Keep your controller clean, but add a line to your routes file
Route::get('order', fn () => to_route('home'));

I have a problem with optional parameters that gives 404 error in laravel 7

I have the following function in laravel 7:
public function create(Franchise $franchise = null)
{
...
}
And the route is as follows:
Route::get('series/create/{franchise?}', 'SerieController#create')->name('serie.create');
when I execute it with parameters for example page_name/series/create/1 it executes normally but when I execute without parameters page_name/series/create I get an error 404 | Not Found.
I also used dd() at the beginning of the function and it keeps giving me the same error
I also did the same with index:
Route::get('series/{franchise?}', 'SerieController#index')->name('serie');
And there it executes me well with or without parameters
Is there a route problem?
it seems you didn't add /series/create to your router. This is the reason why Laravel doesn't see this route and throws a 404 error. Add that route to your router file:

Laravel router problem when I use Route::any

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?

Laravel two similar routes, second route not found

Laravel 5.8, I have two routes:
Route::get('/exam-info/{id}', 'ExamController#mainExamInfo')->name('main_exam_info');
Route::get('/exam/{id}', 'ExamController#startMainExam')->name('start_main_exam');
When I go to the second route it gives me "page not found" error! Why?
Update:
In ExamController my startMainExam function is:
public function startMainExam($exam_id){
dd("sss");
// other stuff
}
But when I change the second route to:
Route::get('/exaxxxx/{id}', 'ExamController#startMainExam')->name('start_main_exam');
It works!!
Do you defined resource route on ExamContorller. Something similar like below on your route.php
Route::resource('exam', 'ExamController');
If it present, please change the second route name to something else like /exam-id/{id}

Call to a member function middleware() on null

I use the following middleware in routing Laravel:
Route::group(['prefix' => 'api/'], function() {
Route::resource('admin', 'adminController')->middleware('auth');
Route::resource('profile', 'profileController')->middleware('role');
});
I get this error when i call 'admin' or 'profile' path in URL
Use this
Route::prefix("/dashboard")->middleware(['first','second'])->group(function(){
});
It is because Route::resource() does not return anything. Its void. It doesn't return an object.
Laravel 5.4 - Illuminate\Routing\Router#resource
In Laravel 5.5 (in development), Route::resource() will be returning an object for fluently adding options to.
Simply reverse the order:
Route::middleware('scope:clock-in')->resource('clock', 'ClockController');
As lagbox stated:
Route::resource() does not return anything.
However middleware does.
Most likely your resource controller is not resolving to an actual controller. Some things to check
Check you actually have an adminController, and that the class name is in the correct case
Check that your controller is in the default namespace and, if not, change the controller namespace, or add a namespace attribute to your route
Check that your controller is not causing an exception on start that is being ignored resulting in you having a null controller.
In Laravel 8.x you can solve this problem with:
Route::group(['middleware' => 'auth','middleware' => 'role'], function () {
..........
});

Resources