I have a group of route that I apply auth Middleware.
How should I except the tournaments.show ????
I only found examples with $this->middleware syntax, but none with Route::group
Route::group(['middleware' => ['auth']],
function () {
Route::resource('tournaments', 'TournamentController', [
'names' => [
'index' => 'tournaments.index',
'show' => 'tournaments.show',
'create' => 'tournaments.create',
'edit' => 'tournaments.edit', 'store' => 'tournaments.store', 'update' => 'tournaments.update' ],
]);
});
You can except the show route from the resource() as:
Route::group(['middleware' => ['auth']],
function () {
Route::resource('tournaments', 'TournamentController',
[
'names' =>
['index' => 'tournaments.index',
'create' => 'tournaments.create',
'edit' => 'tournaments.edit',
'store' => 'tournaments.store',
'update' => 'tournaments.update'
],
'except' => ['show'],
]
);
});
And then define it outside the group as:
Route::get('tournaments/{id}', 'TournamentController#show')->name('tournaments.show');
Related
Here is a API routing with sanctum:
Route::group(["middleware" => "auth:sanctum"], function () {
Route::apiResources([
'profile' => ProfileController::class,
'specialization' => SpecializationController::class,
'specialization/filter' => SpecializationController::class,
'location' => LocationController::class,
]);
});
When I ask any controller it returns a response despite the user is not authorized.
Why Route::group(["middleware" => "auth:sanctum"], function () {} does not work?
I believe the value for the middleware key should be an array:
Route::group(["middleware" => ["auth:sanctum"]], function () {
Route::apiResources([
'profile' => ProfileController::class,
'specialization' => SpecializationController::class,
'specialization/filter' => SpecializationController::class,
'location' => LocationController::class,
]);
});
I want to use a different (plural) model name for my route names, because my url is in a different language.
I can Achieve it with Restful Naming Resource Routes
Route::resource('/foo', BarController::class)->parameters([
'foo' => 'bar',
])->names([
'index' => 'bar.index',
'create' => 'bar.create',
'store' => 'bar.store',
'show' => 'bar.show',
'edit' => 'bar.edit',
'update' => 'bar.update',
'destroy' => 'bar.destroy',
]);
But.. I would have to do it for every resource route:
Route::resource('/usuarios', UserController::class)->parameters([
'usuarios' => 'user',
])->names([
'index' => 'user.index',
'create' => 'user.create',
'store' => 'user.store',
'show' => 'user.show',
'edit' => 'user.edit',
'update' => 'user.update',
'destroy' => 'user.destroy',
]);
How could I make this DRY?
You can extract that part into a function.
function routeNames($model)
{
return array_map(
fn ($n) => "{$model}.{$n}",
['index', 'create', 'store', 'show', 'edit', 'update', 'destroy']
);
}
Route::resource('/usuarios', UserController::class)->parameters([
'usuarios' => 'user',
])->names(routeNames('user'));
In my Laravel 8 app I want to make route of ads/ad_locations editor
Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
...
Route::group(['prefix' => 'ads'], function ($router) {
...
Route::resource(
'/{ad_id}/ad_locations',
AdLocationController::class
)->name('admin.ads.ad_locations');
But I got error clearing routes :
$ php artisan route:cache
ArgumentCountError
Too few arguments to function Illuminate\Routing\PendingResourceRegistration::name(), 1 passed in /mnt/_work_sdb8/wwwroot/lar/AdsBackend8/routes/web.php on line 112 and exactly 2 expected
at vendor/laravel/framework/src/Illuminate/Routing/PendingResourceRegistration.php:110
106▕ * #param string $method
107▕ * #param string $name
108▕ * #return \Illuminate\Routing\PendingResourceRegistration
109▕ */
➜ 110▕ public function name($method, $name)
111▕ {
112▕ $this->options['names'][$method] = $name;
113▕
114▕ return $this;
1 routes/web.php:112
Illuminate\Routing\PendingResourceRegistration::name()
+3 vendor frames
5 routes/web.php:119
Illuminate\Support\Facades\Facade::__callStatic()
Which syntax is valid and how to ref in in blade file ?
MODIFIED BLOCK:
I modified in routes/web.php :
Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
Route::group(['prefix' => 'ads'], function ($router) {
Route::resource('/{ad_id}/ad_locations', AdLocationController::class, [
'names' => [
'index' => 'admin.ads.ad_locations.index',
'store' => 'admin.ads.ad_locations.store',
'edit' => 'admin.ads.ad_locations.edit',
]
]);
and run command with success :
php artisan route:cache
But in blade file using it :
<a class="btn btn-primary" href="{{ route('admin.ads.ad_locations.edit',[$ad_id, $nextAdLocation['id']]) }}">
{!! showAppIcon('edit') !!} Edit
</a>
I got error :
(Symfony\\Component\\Routing\\Exception\\RouteNotFoundException(code: 0): Route [admin.ads.ad_locations.edit] not defined. at /mnt/_work_sdb8/wwwroot/lar/AdsBackend8/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php:429)
[stacktrace]
Which way is correct ?
Thanks!
Function name() doesn't work with route::resource, because it's a collection of routes, it should be used with individual routes like:
Route::get('user/create', [UserController::class, 'create'])->name('user.create');
You can either supply a "names" array as the third parameter (options) parameter to the resource route, like:
Route::resource('user', UserController::class, [
'names' => [
'index' => 'admin.ads.ad_locations.index',
'store' => 'admin.ads.ad_locations.store',
// etc...
]
]);
or, you can use with as keyword, like the one you have used in your route:
Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.ads.ad_locations.'], function () {
Route::group(['prefix' => 'ads'], function ($router) {
Route::resource('/{ad_id}/ad_locations', AdLocationController::class);
});
});
In the above snippet, I have used your complete route "admin.ads.ad_locations" name in "as" attribute's value. But, this last one will be treated as prefix for every single route like this:
admin.ads.ad_locations.{ad_id}.store
admin.ads.ad_locations.{ad_id}.create
...
You can also, use function names() like below:
Route::resource('/{ad_id}/ad_locations', PhotoController::class)->names([
'create' => 'admin.ads.ad_locations.build'
'show' => 'admin.ads.ad_locations.view'
]);
Answer For Rectified Section of the Question:
Because you are using "as" attribute in your route as well, the route names generated for you will be:
admin.admin.ads.ad_locations.store
admin.admin.ads.ad_locations.create
admin.admin.ads.ad_locations.create
You can either remove the as attribute from your route, like:
Route::group(['middleware' => ['auth'], 'prefix' => 'admin'], function () {
Route::group(['prefix' => 'ads'], function ($router) {
Route::resource('/{ad_id}/ad_locations', AdLocationController::class, [
'names' => [
'index' => 'admin.ads.ad_locations.index',
'store' => 'admin.ads.ad_locations.store',
'edit' => 'admin.ads.ad_locations.edit',
]
]);
});
});
or, you can avoid using "admin." in your routes names, like:
Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
Route::group(['prefix' => 'ads'], function ($router) {
Route::resource('/{ad_id}/ad_locations', AdLocationController::class, [
'names' => [
'index' => 'ads.ad_locations.index',
'store' => 'ads.ad_locations.store',
'edit' => 'ads.ad_locations.edit',
]
]);
});
});
i got this method on my routes.php:
Route::resource('maintenance/templates', 'TemplateController', ['names' => createRouteNames('fleet.maintenance.templates')]);
But, i understand, this method break in laravel 5 so, How can i upgrade this method? I understand i need use Route::group( but, i don't know how.
This is one of the tries i did, but, it didn't work:
Route::group(['maintenance/templates' => 'TemplateController'], function(){
Route::resource('template/config', 'ConfigController',[
'only' => ['store', 'update', 'destroy'],
'names' => createRouteNames('fleet.template.config'),
]);
Route::controller('template', 'TemplateController', [
'getTemplates' => 'api.template',
'postService' => 'api.template.service',
]);
});
You don't need the group for the resource function. You can achieve it like this:
Route::resource('maintenance/templates', 'TemplateController', [
'only' => [
'store', 'update', 'destroy'
],
'names' => [
'store' => 'maintenance/templates.store',
'update' => 'maintenance/templates.update',
'destroy' => 'maintenance/templates.destroy',
]
]);
Or you can pass a callable that will return an associative array like the example above:
Route::resource('maintenance/templates', 'TemplateController', [
'only' => [
'store', 'update', 'destroy'
],
'names' => createRouteNames('fleet.maintenance.templates')
]);
Please I need help with how my links to the chatter forum are truncated when I click on discussions or any other link on the forum homepage.
A quick solution would be very much appreciated.Kindly Check the URL
Here's an example:
instead of;
localhost/apps/school/forums/discussions
it shows as:
localhost/forums/discussions
Below is the Route/web.php
<?php
/**
* Helpers.
*/
// Route helper.
$route = function ($accessor, $default = '') {
return $this->app->config->get('chatter.routes.'.$accessor, $default);
};
// Middleware helper.
$middleware = function ($accessor, $default = []) {
return $this->app->config->get('chatter.middleware.'.$accessor, $default);
};
// Authentication middleware helper.
$authMiddleware = function ($accessor) use ($middleware) {
return array_unique(
array_merge((array) $middleware($accessor), ['auth'])
);
};
/*
* Chatter routes.
*/
Route::group([
'as' => 'chatter.',
'prefix' => $route('home'),
'middleware' => $middleware('global', 'web'),
'namespace' => 'DevDojo\Chatter\Controllers',
], function () use ($route, $middleware, $authMiddleware) {
// Home view.
Route::get('/', [
'as' => 'home',
'uses' => 'ChatterController#index',
'middleware' => $middleware('home'),
]);
// Single category view.
Route::get($route('category').'/{slug}', [
'as' => 'category.show',
'uses' => 'ChatterController#index',
'middleware' => $middleware('category.show'),
]);
/*
* Auth routes.
*/
// Login view.
Route::get('login', [
'as' => 'login',
'uses' => 'ChatterController#login',
]);
// Register view.
Route::get('register', [
'as' => 'register',
'uses' => 'ChatterController#register',
]);
/*
* Discussion routes.
*/
Route::group([
'as' => 'discussion.',
'prefix' => $route('discussion'),
], function () use ($middleware, $authMiddleware) {
// All discussions view.
Route::get('/', [
'as' => 'index',
'uses' => 'ChatterDiscussionController#index',
'middleware' => $middleware('discussion.index'),
]);
// Create discussion view.
Route::get('create', [
'as' => 'create',
'uses' => 'ChatterDiscussionController#create',
'middleware' => $authMiddleware('discussion.create'),
]);
// Store discussion action.
Route::post('/', [
'as' => 'store',
'uses' => 'ChatterDiscussionController#store',
'middleware' => $authMiddleware('discussion.store'),
]);
// Single discussion view.
Route::get('{category}/{slug}', [
'as' => 'showInCategory',
'uses' => 'ChatterDiscussionController#show',
'middleware' => $middleware('discussion.show'),
]);
// Add user notification to discussion
Route::post('{category}/{slug}/email', [
'as' => 'email',
'uses' => 'ChatterDiscussionController#toggleEmailNotification',
]);
/*
* Specific discussion routes.
*/
Route::group([
'prefix' => '{discussion}',
], function () use ($middleware, $authMiddleware) {
// Single discussion view.
Route::get('/', [
'as' => 'show',
'uses' => 'ChatterDiscussionController#show',
'middleware' => $middleware('discussion.show'),
]);
// Edit discussion view.
Route::get('edit', [
'as' => 'edit',
'uses' => 'ChatterDiscussionController#edit',
'middleware' => $authMiddleware('discussion.edit'),
]);
// Update discussion action.
Route::match(['PUT', 'PATCH'], '/', [
'as' => 'update',
'uses' => 'ChatterDiscussionController#update',
'middleware' => $authMiddleware('discussion.update'),
]);
// Destroy discussion action.
Route::delete('/', [
'as' => 'destroy',
'uses' => 'ChatterDiscussionController#destroy',
'middleware' => $authMiddleware('discussion.destroy'),
]);
});
});
/*
* Post routes.
*/
Route::group([
'as' => 'posts.',
'prefix' => $route('post', 'posts'),
], function () use ($middleware, $authMiddleware) {
// All posts view.
Route::get('/', [
'as' => 'index',
'uses' => 'ChatterPostController#index',
'middleware' => $middleware('post.index'),
]);
// Create post view.
Route::get('create', [
'as' => 'create',
'uses' => 'ChatterPostController#create',
'middleware' => $authMiddleware('post.create'),
]);
// Store post action.
Route::post('/', [
'as' => 'store',
'uses' => 'ChatterPostController#store',
'middleware' => $authMiddleware('post.store'),
]);
/*
* Specific post routes.
*/
Route::group([
'prefix' => '{post}',
], function () use ($middleware, $authMiddleware) {
// Single post view.
Route::get('/', [
'as' => 'show',
'uses' => 'ChatterPostController#show',
'middleware' => $middleware('post.show'),
]);
// Edit post view.
Route::get('edit', [
'as' => 'edit',
'uses' => 'ChatterPostController#edit',
'middleware' => $authMiddleware('post.edit'),
]);
// Update post action.
Route::match(['PUT', 'PATCH'], '/', [
'as' => 'update',
'uses' => 'ChatterPostController#update',
'middleware' => $authMiddleware('post.update'),
]);
// Destroy post action.
Route::delete('/', [
'as' => 'destroy',
'uses' => 'ChatterPostController#destroy',
'middleware' => $authMiddleware('post.destroy'),
]);
});
});
});
/*
* Atom routes
*/
Route::get($route('home').'.atom', [
'as' => 'chatter.atom',
'uses' => 'DevDojo\Chatter\Controllers\ChatterAtomController#index',
'middleware' => $middleware('home'),
]);
You need to set the root directory to point to the public folder in laravel for the url rewriting to work and this won't happen when you're using xampp/wamp and accessing the url like folder structure. Only the index page of laravel app would work and other pages would throw errors. Also the links generated would not be accurate since laravel uses the base app url.
You should run php artisan serve and access your app with http://localhost:8000. This solves your routing and url generating issues. The other option would be to modify the virtual hosts in your local installation which isn't as easy.