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) {
...
});
Related
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');
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 need to use facebook api, and it requires me to have https, so I want to force my laravel project to use it.
With Laravel I use react scafffolding also I run project on Xampp
I already tried
setting 'url' => 'https://localhost' in config/app.php
putting HTTPS=true APP_URL=https://localhost in .env
putting \Illuminate\Support\Facades\URL::forceScheme('https'); if($this->app->environment('production')) {
\URL::forceScheme('https');
} in boot method at appServiceProvider.php
my url seems not to change, it always says localhost:8000
My laravel project is not small, I don't know how mutch code is needed to reprocuce same issue, inform, and I'll provide anything
I'm all set up with Passport in 5.5 and have the auto generated Auth\ForgotPasswordController and Auth\ResetPasswordController controllers.
However whereas /oauth/token was provided magically for me, there don't appear to be such routes for password reset when using the API.
What should my API routes look like?
Currently I've experimented with
Route::group(['prefix' => 'password'], function () {
Route::post('/email', 'Auth\ForgotPasswordController#sendResetLinkEmail');
Route::post('/reset', 'Auth\ResetPasswordController#reset');
});
but I found these in the vendor files when looking at the traits and aren't sure if this is the correct way.
The /password/email route also fails with "message": "Route [password.reset] not defined."
since you don't see any route other then 2 custom, therefore i am assumin you havn't run artisan auth command. First run that. it will add lot of routes in ur project.
Then set api driver to passport.
I have defined a route action with some business logic, inside an internally developed package. Depending on the result in this action, the app want to redirect the user to some dynamic route (Redirect::route('admin.index', [$app->id]) e.g).
How would I do this?
Any solution I come up with doesn't work because of the way Laravel handles routes.
Right now I have copied the route to the app routes.php, and extracted the business logic to a method inside the package. But this is not optimal, as I'd like to also keep the route inside the package.
Laravel has some documentation on Package Configuration that should work for you.
In your package's src/config/config.php:
<?php
return array(
'route_admin_index' => 'admin.index',
);
Change your package's code to:
Redirect::route(Config::get('your_package_name::route_admin_index'), [$app->id]);
Now when installed on different environments, you can do:
php artisan config:publish your_vendor_name/your_package_name
Which will publish your package's configuration file to:
app/config/packages/your_vendor_name/your_package_name
Where then you can change the route_admin_index at will.
If php artisan config:publish was not called. Your route will default back to what you have in your package's config file.