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.
Related
I am making a website with laravel but i am getting an error while using middleware auth. it says Route[x] not defined.
i am using laravel 5.8 please some one help me
The route you have written that has no named route so you need to specify name for that route like this
Route::get('x', function() {
//
})->name('x')->middleware('auth');
for more information you can check here https://laravel.com/docs/7.x/urls#urls-for-named-routes
I just updated my application into version 5.3 and in many routes I'm getting
trying to get property of non-object
Like for example I have a resource called post and another called Article where each post can have one Articles. And each Article has one writer. When I do
$article->post->writer;
I get trying to get property 'writer' of non-object
Before updating from 5.2 this route as well as other routes worked perfectly. Am i missing something here? should i do something after composer update has been run successfully?
Edit: The article, the post and the writer all exist in the DB. When I try to return $article I get this.
{
"article": []
}
When i switch back to laravel 5.2 I get the proper article
Edit 2: it is routing bidnings problem. I'm using implicit model binding, so when i pass the ID of the article to my route, Laravel fetches the resource. but Now apparently it doesn't do it. I followed the instructions here for the bindings https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0 but Laravel can't still get the article
It was name bindings issue. Laravel 5.3 needs some configuration in kernel.php as well as in the group route https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0
The group should have 'middleware' => ['bindings']
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');
Laravel 5.5
which is the different doing (no get and post methods) on route defintion web.php file:
$this->get('login', 'Auth\LoginController#showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController#login');
regarding ->name('') method
Is required define that method? in which cases?
(sample taked from Auth Class definition laravel)
The idea of defining ->name() in routes is for easier code maintenance in the future, it's not mandatory.
Say for example you have few places which uses the route login, one fine day you update the route to user-login. You will have to find and update all the route being used, changing from url('login') to url('user-login').
If you have a route name defined, you will be using route('login'), when you update your route url, there's no need to update all the other files that you're using that route.