LogicException : Unable to prepare route [api/user] for serialization. Uses Closure.
at /var/www/html/dev_laravel/vendor/laravel/framework/src/Illuminate/Routing/Route.php:917
913| */
914| public function prepareForSerialization()
915| {
916| if ($this->action['uses'] instanceof Closure) {
> 917| throw new LogicException("Unable to prepare route [{$this->uri}] for serialization. Uses Closure.");
918| }
919|
920| $this->compileRoute();
921|
Exception trace:
1 Illuminate\Routing\Route::prepareForSerialization()
/var/www/html/dev_laravel/vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteCacheCommand.php:62
2 Illuminate\Foundation\Console\RouteCacheCommand::handle()
/var/www/html/dev_laravel/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:32
When i trying to run the laravel command
php artisan route:cache
I try to find out the solution but not get the correct solution.
https://github.com/laravel/framework/issues/22034
Is this laravel bug still or fixed the bug?
I have the code on web.php file
Route::get('/', function () {
return view('welcome');
});
Route::resource('photos', 'PhotoController#index');
I'm Using Laravel 5.8. Just installed and migrated database. I'm beginner for laravel.
Can anyone let me know the correct solution?
Thanks in advance
It is not a bug. You can not use Closure based routes like that if you want to cache the routes. Direct any Closure based route to a Controller method instead and you will be fine. [You have one in web.php and the error is pointing out one in api.php]
The Closure based route you have in web.php could be replaced with:
Route::view('/', 'welcome');
This would direct it to a Controller that just handles returning view files.
The Closure based route in api.php should point to a Controller:
Route::middleware('auth:api')->get('user', 'SomeController#user');
Consider any routes that come with the laravel/laravel project as being functional but are there for demonstration purposes.
"Closure based routes cannot be cached. To use route caching, you must convert any Closure routes to controller classes."
Laravel 5.8 - Docs - Controllers - Route Caching
EDIT:
AS OF LARAVEL 8.X YOU CAN ALSO CACHE CLOSURE BASED ROUTES
When you run the command php artisan route:cache
Laravel will cache all your routes and store it in specified Cache Driver
Now Comming to your Error Message:
As the error Message Clearly Says that
Closure Routes can't be Cached
And even the Laravel Docs says that Route Caching
By Default Laravel Comes with Four routes files
And this will have 2 Closure based routes
Solution:
You can remove them if you no longer using that routes
Make a Controller and Move that to Controller
LogicException : Unable to prepare route [api/user]
Which means that
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
This Code in Your routes/api.php is causing the issue So
Route::middleware('auth:api')
->get('/user', 'SomeController#method');
Or you can remove that if not using that
You could not use specific method when you use resource for the routing.
You can use
Route::resource('photos', 'PhotoController');
Or
Route::get('photos', 'PhotoController#index');
Related
Here is the problem.
I started a dingo project and am using laravel-modules in it. Every module has its own routing files. Using the project in development environment, everything works fine.
But when I run php artisan config:cache, when a request comes to laravel, it return the response The version given was unknown or has no registered routes. As I see, the problem is dingo just check the default api.php and web.php files to find the route. But module routes are not stored in that files. I store them in Modules/module_name/route/api.php file (as laravel-modules suggested).
Any suggestion would be welcome.
change api file of module with version param in group session like this:
$api = app('Dingo\Api\Routing\Router');
$api->group(['version' => 'v1'], function ($api) {
...
});
When I commenting all routes I getting this error.
Please say me, what are the closure routes? And why showing this error
A closure is an anonymous function which is used to define routes without an action:
Route::get('/test', function () {
return 'hello world';
});
The example above returns hello world when you call the route /test in your browser.
You can't cache routes that are referencing a closure.
There are two routes using closures in a default Laravel app: in the files routes/api.php and routes/web.php. Remove them or move them to a controller and you are able to cache your routes.
Here's an issue in the Laravel framework repository which is discussing this behaviour.
I'm building and application in Laravel and Vuejs where I'm having Laravel routes as below:
Route::get('/admin/{view?}', 'HomeController#admin')->where('view', '(.*)')->name('admin');
Route::get('/{view?}', 'HomeController#home')->where('view', '(.*)')->where('view', '!=', 'admin')->name('home');
I'm using Vue-router so I'm having routing in vuejs, and I'm using history mode. The problem is when I try to call /admin it generally calls HomeController#home method. even if I go deeper like /admin/dashboard it is calling the same home method. I want if admin prefix is being called then it should call HomeController#admin method.
its all okay for me please check this
Route::get('/admin/{view?}', function (){
dd('okay');
})->where('view', '(.*)')->name('admin');
Route::get('/{view?}', function(){
dd('okay1');
})->where('view', '(.*)')->name('home');
So try this
Route::get('/admin/{view?}', 'HomeController#admin')->where('view', '(.*)')->name('admin');
Route::get('/{view?}', 'HomeController#home')->where('view', '(.*)')->name('home');
I am getting the error below when trying to access the route, but the controller is needed to load the data:
(1/1) InvalidArgumentException
Action Facade\Ignition\Http\Controllers\ShareReportController not defined.
I am using Tenancy/Multi-Tenant package and I have configured it to use routes/tenants.php to load routes specifically for tenants. If I do the following in the tenants.php file, it returns the proper response.
Route::get('/test', function() {
return 'Test success';
});
though when I try to do the same, but loading the data from a controller such as this:
Route::get('/testt', 'TenantController#testt');
It will show the error:
(1/1) InvalidArgumentException
Action Facade\Ignition\Http\Controllers\ShareReportController not defined.
If i try to put the same code in web.php routes, then it works perfectly. What could be the problem? Is it something in my code? Can it be because of the multi-tenant package i'm using? How would i go about further debugging this?
Can you see if your routes are cached and try clearing that cache. Just clear project route-cache using route:clear
The fix was to group the routes in tenants.php with the web middleware and a namespace:
Route::middleware('web')->namespace('App\Http\Controllers')->group(function() {
//Routes
});
Try composer dump-autoload -o
it helped for me.
After some minutes trying to fix I found the solution.
You don't need to group the routes if you've done in RoutesServiceProvider or in a custom Provider.
Just go to config/tenancy.php and go to routes -> path, Remove the base_path() function and let the string:
'path' => base_path('routes/tenants/tenants.php'),
to
'path' => 'routes/tenants/tenants.php',
And this error should be fixed.
I had a similar error after install laravel/passport 8.1 in Laravel 6.2:
Action Facade\Ignition\Http\Controllers\ExecuteSolutionController not defined.
Fixed it runnig composer update. The result was:
Updating facade/ignition (1.13.0 => 1.13.1):
For people finding this via Google: I had a similar error with Laravel 6.5. I had messed up my AppServiceProvider with an incomplete Git merge:
<?php
namespace App\Providers;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Blade;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
<<<<<<< HEAD
Blade::if(
'iscurrentroute',
function ($route) {
$route = Str::replaceFirst(Request::getSchemeAndHttpHost().'/', '', $route);
return Request::is($route);
}
);
=======
Blade::component('components.sortable', 'sortable');
>>>>>>> feature/WLI-58-bedrijf-beheren
}
}
Deleting the "='s", "<'s", and ">'s", and the double use of Blade fixed it for me.
on server side I went to /stoage folder and cleared cashs. E.g. views folder inside that /storage. Then additionally changed all entire folders' and files' permissions to can read and write.
Then pages started show up as expected
I was using the Ignition error pages in Laravel and I have to say that I much prefer the whoops package.
I had the same error reported in this question and by chance changing the error package installed in my app to the whoops one showed me the real error my app was having and I was then instantly able to resolve it. So it seemed that ignition wasn't exactly the cause but it was hindering me resolving another issue.
I upgrade Laravel 5.1 to 5.3 and have some problem with routes.
In Laravel 5.1 I have route like:
Route::controllers([
'pages/{page_type}' => 'Admin\AdminPagesController',
]);
And in controller I have methods like:
getIndex($type)
postIndex($type, Request $request)
getAdd($type)
postAdd(Request $request)
getEdit($type, $id)
postEdit(Request $request, $id) and others...
But in 5.3 when I created routes:
Route::resource('pages/{page_type}', 'Admin\AdminPagesController');
I got an error
NotFoundHttpException in RouteCollection.php line 161:
or
Route pattern "/master/pages/{page_type}/{{page_type}}" cannot reference variable name "page_type" more than once.
and it generate me route in RESTful
Can anyone help me?
Thanks.
Since there is not alternative to ::controller you need to create separate route for each action if you don't want to use rest:
Route::get('pages/{page_type}', 'Admin\AdminPagesController#getIndex');
Route::post('pages/{page_type}', 'Admin\AdminPagesController#postIndex');
....
Seems that Route::controllers method has been removed in Laravel 5.2, I can't find it in the documentation since then, and doesn't exist in the Illuminate\Routing\Router.php file in Laravel 5.3
You will have to create each route separately for your case. Or you can simply use the Route::resource method, what do you have against it? You can add extra methods to a resource declaring them before the Route::resource call.