Laravel 8 how to name a group - php

This question has been asked but it's outdated in most answers I saw.
This is what I'm trying:
Route::prefix('cart')->group(function() {
Route::get('/mycart','App\Http\Controllers\frontend\CartController#mycart')->name('mycart');
Route::get('/checkout','App\Http\Controllers\frontend\CartController#checkout')->name('checkout');
})->name('cart');
I'd like to name the group and then in my blade be able to do:
#if (Route::currentRouteName() != 'individual_product')
The above gives me this error: Call to a member function name() on null
Any idea what i'm doing wrong?

One can configure everything with arrays:
use Illuminate\Support\Facades\Route;
Route::group(['prefix' => 'cart', 'name' => 'cart.'], function() {
Route::get('/index', ['as' => 'index', 'uses' => 'CartController#index']);
Route::get('/checkout', ['as' => 'checkout', 'uses' => 'CartController#checkout']);
});
And 'as' => 'cart.index' might still be more readable than a named group (it may depend).
In Blade one can also use Route:
#if (Route::has('cart')) #endif
#if (Route::has('cart.index')) #endif
Listing all defined routes is being supported:
php artisan route:list

You can use name prefixes like below:
Route::name('cart.')->prefix('cart')->group(function() {
Route::get('/mycart','App\Http\Controllers\frontend\CartController#mycart')->name('mycart');
Route::get('/checkout','App\Http\Controllers\frontend\CartController#checkout')->name('checkout');
});
for more : https://laravel.com/docs/8.x/routing#route-group-name-prefixes
in blade you can check
#if(request()->routeIs('cart.*')) // or ->routeIs('cart.mycart')
#endif
https://laravel.com/docs/8.x/requests#retrieving-the-request-path

Related

generate route for routes defined inside group

I have this route defined inside a group
Route::group(['domain' => '{subdomain}.test.com'], function () {
Route::get('/models/{id?}', [
'as' => 'car-model',
'uses' => 'CarModelController#details'
]);
});
I want to avoid hardcoding URLs in blade
{{route('car-model', 'ford', '100) }}
but that returs this url
ford.test.com/models
no model id!
Not sure if is relevant but in my controller CarModelController.php
I defined
public function details($subdomain, $id)
why is not sending the id to the generated url? Do I need to send the $subdomain parameter to the detail function?
I found that
{{route('car-model', ['make' => 'ford', 'id' => '100]) }}
works! thanks for watching :)

Laravel 5 new routes don't work

I'm starting with laravel 5 framework and I have a problem with the routes.
In the last few days, the routes worked correctly, but today I added a new route and it doesn't work anymore.
I have these routes
Route::get('url/create', 'UrlController#create');
Route::get('url/bulk', 'UrlController#bulk_view');
Route::post('url/bulk', ['as' =>'url/bulk', 'uses' => 'UrlController#bulk']);
Route::get('url/bulk_metrics', 'UrlController#bulk_metrics_view');
Route::post('url/bulk_metrics', ['as' =>'url/bulk_metrics', 'uses' => 'UrlController#bulk_metrics']);
Route::post('url/create', ['as' =>'url/create', 'uses' => 'Urlcontroller#store']);
Route::post('url/update/{id}', ['as' =>'url/update', 'uses' => 'Urlcontroller#update']);
Route::get('urls', ['as' =>'url/list', 'uses' => 'Urlcontroller#index']);
Route::get('url/{id}', ['as' =>'url/show', 'uses' => 'Urlcontroller#show']);
Route::post('url/delete/{id}', ['as' =>'url/delete', 'uses' => 'Urlcontroller#destroy']);
All work correctly, but I added this new route
Route::post('urls/filter', ['as' =>'url/filter', 'uses' => 'Urlcontroller#filter']);
and I call it like this
{!! Form::open(array('route' => 'urls/filter', 'method' => 'POST')) !!}
I tried php artisan route:clear, php artisan route:cache and php artisan route:list, and the new route appear in the list:
POST | urls/filter | url/filter | App\Http\Controllers\Urlcontroller#filter | web,auth |
The other routes work correctly and I think that it is a cache problem, because if I change the url/create to url/create2, and I change it in the template to url/create2 it doesn't work.
Thanks in advance to all
You should use it as url/filter
{!! Form::open(array('route' => 'url/filter', 'method' => 'POST')) !!}
because you're naming it like this:
'as' =>'url/filter'
Or remove 'as' =>'url/filter' part from route. In this case name of your route will be urls/filter and not url/filter.

Named Routes Conflict Laravel 5.2

I am creating a Multilingual Laravel 5.2 application and I am trying to figure out how to change language when I already have the content.
routes.php
...
Route::get('home', [
'as' => 'index',
'uses' => 'SiteController#home'
]);
Route::get([
'as' => 'index',
'uses' => 'SiteController#inicio'
]);
...
I have SiteController#home and SiteController#inicio. So I change session('language') in SiteController#change_language like:
...
public function change_language ($lang){
session(['language' => $lang]);
return redirect()->action(SAME NAMED ROUTE, DIFFERENT LANGUAGE);
}
...
So, When I click on a button with
English
from /inicio (SiteController#inicio) I should be redirected to the same named route (SiteController#home) so I can check the language and display appropriate content.
Any ideas of how to get the named route or something helpful?
Thank you :)

Laravel - Get route name in filter

How can I get current route name in filter? I tried use Route::currentRouteName(); but it's null.
Route::filter('belongsToUser', function(){
dd( Route::currentRouteName() );
exit;
});
Route looks for example:
Route::get('/openTicket/{id}', array('before' => 'auth|belongsToUser', 'uses' => 'MyController#MyAction'));
Your route isn't named, so it's no surprise the route name is null. You need an as parameter.
Route::get('/openTicket/{id}', array(
'as' => 'yourRouteName',
'before' => 'auth|belongsToUser',
'uses' => 'MyController#MyAction'));
http://laravel.com/docs/routing#named-routes

Laravel 4.1 Route::controller inside a name spaced route group does not work

It seems that adding
Route::controller('acme','Acme');
inside a name spaced route group does not work and i have to take it out of the whole group
check the code below
// this code does not work, error message:
//
// --------------------
// ReflectionException
// Class Api\Controllers\V1\Api\Controllers\V1\Acme does not exist
// --------------------
//
// the error appears after adding Route::controller('acme', 'Acme'); inside the name spaced route group
Route::group(['prefix' => 'api','namespace' => 'Api\Controllers'], function()
{
Route::group(['prefix' => 'v1','namespace' => 'V1'], function()
{
Route::resource('acme', 'Acme', [ 'only' => ['index', 'show', 'store', 'update', 'destroy'] ]);
Route::controller('acme', 'Acme');
});
});
// this code is working fine after taking Route::controller('acme', 'Acme'); outside the name spaced route group
Route::group(['prefix' => 'api','namespace' => 'Api\Controllers'], function()
{
Route::group(['prefix' => 'v1','namespace' => 'V1'], function()
{
Route::resource('acme', 'Acme', [ 'only' => ['index', 'show', 'store', 'update', 'destroy'] ]);
});
});
Route::controller('acme', 'Api\Controllers\V1\Acme');
you can view the code here if you prefer it more readable
http://paste.laravel.com/1inX
is it a bug or am i missing some thing ?
Yes, this is a bug. Just reproduced it here to confirm.
And posted this issue: https://github.com/laravel/framework/issues/3084
UPDATE
It works now, Taylor just killed that bug.

Categories