When I clear caches in my Laravel 5.2 project, I see this error message:
[LogicException]
Unable to prepare route [panel] for serialization. Uses Closure.
I think that it's related with a route
Route::get('/article/{slug}', 'Front#slug');
associated with a particular method in my controller:
public function slug($slug) {
$article = Article::where('slug',$slug)->first();
$id = $article ->id_article ;
if ( ($article=== null) || (is_null($id)) ) return view('errors/Db');
else return view('detail')->with(array('article'=> $article, 'title'=>'My title - '.$article->title));
}`
In short, from a master view I pass $slug, that is a shortlink to the article, with $slug , which is unique in the database, I identify the record and then I pass it's contents to the detail view.
I didn't have any problem when I wrote the method, infact it worked like a charm, but after I cleaned caches, I get that error and the links in the master view don't show any shortcode.
Where am I doing wrong?
I think that it's related with a route
Route::get('/article/{slug}', 'Front#slug');
associated with a particular method in my controller:
No, thats not it. The error message is coming from the route:cache command, not sure why clearing the cache calls this automatically.
The problem is a route which uses a Closure instead of a controller, which looks something like this:
// Thats the Closure
// v
Route::get('/some/route', function() {
return 'Hello World';
});
Since Closures can not be serialized, you can not cache your routes when you have routes which use closures.
If none of your routes contain closures, but you are still getting this error, please check
routes/api.php
Laravel has a default auth api route in the above file.
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
which can be commented or replaced with a call to controller method if required.
This is definitely a bug.Laravel offers predefined code in routes/api.php
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
which is unabled to be processed by:
php artisan route:cache
This definitely should be fixed by Laravel team.(check the link),
simply if you want to fix it you should replace routes\api.php code with some thing like :
Route::middleware('auth:api')->get('/user', 'UserController#AuthRouteAPI');
and in UserController put this method:
public function AuthRouteAPI(Request $request){
return $request->user();
}
The Actual solution of this problem is changing first line in web.php
Just replace Welcome route with following route
Route::view('/', 'welcome');
If still getting same error than you probab
the solustion when we use routes like this:
Route::get('/', function () {
return view('welcome');
});
laravel call them Closure so you cant optimize routes uses as Closures you must route to controller to use php artisan optimize
Check your routes/web.php and routes/api.php
Laravel comes with default route closure in routes/web.php:
Route::get('/', function () {
return view('welcome');
});
and routes/api.php
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
if you remove that then try again to clear route cache.
If you're coming to this problem because you've upgraded Laravel <5.8 project up to >=5.8, you've likely used the ./vendor/bin/carbon-upgrade method to upgrade the project as suggested by your terminal. In this case, you simply need to remove the following two blocks from the bottom of your composer.json file and composer install again:
"post-install-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postInstall",
"php artisan optimize"
],
"post-update-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postUpdate",
"php artisan optimize"
],
If someone is still looking for an answer, for me the problem was in routes/web.php file. Example:
Route::get('/', function () {
return view('welcome');
});
It is also Route, so yeah...Just remove it if not needed and you are good to go!
You should also follow answers provided from above.
In order to troubleshoot this (at least in laravel 6):
The action property inside Route.php has all the info needed. A better error message should be possible to provide by laravel.
What I did was to add a dd($this->action) just before the exception is thrown here:
https://github.com/laravel/framework/blob/6.x/src/Illuminate/Routing/Route.php#L917
With that in place I could easily pinpoint the location, in my case api.php and lines 22-24:
array:6 [
"middleware" => "api"
"domain" => "local-api.mydomain.com"
"uses" => Closure()^ {#6497
class: "App\Providers\RouteServiceProvider"
this: App\Providers\RouteServiceProvider {#5743 …}
file: "./routes/api.php"
line: "22 to 24"
}
"namespace" => "App\Http\Controllers"
"prefix" => null
"where" => []
]
This is how I solved mine.
Navigate to routes directory
Then open api.php
comment code that looks like this:
Route::middleware('auth:api')->get('/user', function (Request $request) { return $request->user(); });
Then run
php artisan optimize
check that your web.php file has this extension
use Illuminate\Support\Facades\Route;
my problem gone fixed by this way.
Related
I'm trying to set up a basic Laravel 9 CRUD application, and I cannot get the edit route working for the User Controller.
routes/web.php
Route::get('/dashboard', function () { return view('dashboard'); })
->middleware(['auth'])->name('dashboard');
use App\Http\Controllers\UserController;
Route::controller(UserController::class)->group(function(){
Route::get('user','index')->middleware('auth')->name('cases');
Route::get('user/create','create')->middleware('auth');
Route::post('user/create','store')->middleware('auth');
Route::get('user/{$id}/edit','edit')->middleware('auth');
Route::post('user/{$id}/edit','update')->middleware('auth');
Route::get('user/{$id}/delete','destroy')->middleware('auth');
Route::get('user/{id}','show')->middleware('auth');
});
require __DIR__.'/auth.php';
UserController.php
class UserController extends Controller
{
function edit(int $id)
{
echo 123;
}
I'm getting a 404 NOT FOUND page
Also, why don't I see the stack trace for this error?
Also, in some examples, I've seen people using the model class name as the parameter type in the controller method declaration, such as:
function edit(User $id)
{
echo 123;
}
However, I've also seen other examples using int instead. So which is the correct one?
First, inside your .env filte you should put
APP_ENV=local
APP_DEBUG=true
and change your web.php to:
<?php
use Illuminate\Support\Facades\Route;
Route::get('/dashboard', function () { return view('dashboard'); })->middleware(['auth'])->name('dashboard');
use App\Http\Controllers\UserController;
Route::controller(UserController::class)->group(function(){
Route::get('user','index')->middleware('auth')->name('cases');
Route::get('user/create','create')->middleware('auth');
Route::post('user/create','store')->middleware('auth');
Route::get('user/{id}/edit','edit')->middleware('auth');
Route::post('user/{id}/edit','update')->middleware('auth');
Route::get('user/{id}/delete','destroy')->middleware('auth');
Route::get('user/{id}','show')->middleware('auth');
});
require __DIR__.'/auth.php';
Then, try to run
php artisan route:list
and check if your routes are correct.
And try to remove middlewares inside user files, maybe you do not have login page and it redirects you there.
Be sure you are in the correct url like localhost/user/1/edit
Parameters in routes don't take a starting $. Change all occurrences of {$id} in your routes to just {id}:
Route::controller(UserController::class)->group(function(){
Route::get('user','index')->middleware('auth')->name('cases');
Route::get('user/create','create')->middleware('auth');
Route::post('user/create','store')->middleware('auth');
Route::get('user/{id}/edit','edit')->middleware('auth');
Route::post('user/{id}/edit','update')->middleware('auth');
Route::get('user/{id}/delete','destroy')->middleware('auth');
Route::get('user/{id}','show')->middleware('auth');
});
More on Route Parameters
Edit: you might also want to take a look at Resource Controllers. Something like Route::resource('users', UserController::class); will manage all of the required routes
I was working in laravel 8.x, I have developed API to register, but when i test using postman also in browser the url [1]: http://127.0.0.1:8000/api/register always returns 404 not found message.
below is my api.php
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::group(['prefix' => 'v1'], function () {
Route::post('/login', 'UsersController#login');
Route::post('/register', 'UsersController#register');
Route::get('/logout', 'UsersController#logout')->middleware('auth:api');
});
can you please help?
Since you already put a route group "v1", all your routes must have that prefix, so just api/register wont work because that route doesn't exist inside your api.php, so infront of your routes just use
http://127.0.0.1:8000/api/v1/register
http://127.0.0.1:8000/api/v1/login
http://127.0.0.1:8000/api/v1/logout
Your routes looking fine.
I think you are forgetting to add http://127.0.0.1:8000/api/**v1**/register so please try with it once.
i'm using laravel 6 and have 2 route in my app; index and dashboard.
My routes/web is:
Auth::routes();
Route::middleware(['auth'])->group(function () {
Route::get('/index', 'todoApp\TodoController#index')->name('index');
Route::get('/dashboard', 'todoApp\Dashboard#dashboard')->name('dashboard');
});
i added dashboard route recently.
Auth::user() is null when i dump it in dashboard route but doesn't in index. What's the
Your Controller is instantiated before the middleware stack has ran; this is how Laravel can know what middleware you have set via the constructor. Because of this you will not have access to the authenticated user or sessions at this point. Ex:
public function __construct()
{
$this->user = Auth::user(); // will always be null
}
If you need to assign such a variable or access this type of information you would need to use a controller middleware which will run in the stack after the StartSession middleware:
public function __construct()
{
$this->middleware(function ($request, $next) {
// this is getting executed later after the other middleware has ran
$this->user = Auth::user();
return $next($request);
});
}
When the dashboard method is called, the middleware stack has already passed the Request all the way through to the end of the stack so all the middleware needed for Auth to be functioning and available has already ran at that point which is why you have access to Auth::user() there.
I think that this has something to do with the 'web' middleware. If you take a look into the Kernel.php (In app\Http) you will find the web middleware group.
This will show you that it actually calls a middleware called StartSession. Based on your route file (where web is not included as a middleware) I would think that you don't have a session in your Controller and there for no access to it.
I don't quite understand why this only happens in your /dashboard route, because the issue should also be in your /index route (unless you added the web middleware somewhere in your TodoController).
I think that this should do the trick:
Route::middleware(['web', 'auth'])->group(function () {
Route::get('/index', 'todoApp\TodoController#index')->name('index');
Route::get('/dashboard', 'todoApp\Dashboard#dashboard')->name('dashboard');
});
If you fire php artisan make:auth command.
It's doesn't matter where you define because of it's only define auth route
Route::middleware(['auth'])->group(function () {
Route::get('/index', 'todoApp\TodoController#index')->name('index');
Route::get('/dashboard', 'todoApp\Dashboard#dashboard')->name('dashboard');
});
Auth::routes();
I cleared every instance of a closure from my web.php file but i'm still getting this error when I run php artisan route:cache.How do I fix this.
The format of the routes that I used are:
Route::get('/home',[
'uses'=>'HomeController#index',
])->name('home');
and the other format is
Route::get('/profile',[
'uses'=>'profileController#index',
]);
Edit: Formatted code
I figured a way around this error its due to Route present in the routes > api.php , just comment it out and files will be cached successfully.
// Route::middleware('auth:api')->get('/user', function (Request $request) {
// return $request->user();
// });
I can't figure out how to go from a store method directly to a edit page.
Routes:
Route::post('/', 'PostController#store')->name('posts.store');
Route::get('/', 'PostController#edit')->name('posts.edit');
post controller:
public function store(Request $request)
{
$post = new Post;
$post->save();
return view('posts.edit');
}
public function edit($id)
{
dd('edit post');
}
I keep getting view not found or other errors. I have checked php artisan route:list and the correct route is there. What am i missing here?
Well you should do a redirect to the route instead. See Redirecting to Named Routes
So this should work:
return redirect()->route('posts.edit');
At this point, you are trying to load a view.