Using content ID and Slug in URI in Laravel route - laravel

Currently I have routes of the type:
lesson/{id}
and I would like to be able to use a slug as well in the path:
lesson/learn-how-to-play
so that both lesson/1 and lesson/learn-how-to-play bring me to the same page.
I Store the slug as a field in the Lesson table.
I have seen ways to use either, or but not both at the same time.
Then is is possible to call route('lesson.index', $lesson_id) and have it render the URI with the slug rather then the lesson ID?

You can use a regular expression to filter your routes with numeric ids:
Route::get(
'lesson/{id}',
['as' => 'lesson.index', 'uses' => 'ControllerName#byId']
)->where('id', '[0-9]+');
and then use the slug one as a fallback:
Route::get(
'lesson/{slug}',
['as' => 'lesson.index', 'uses' => 'ControllerName#bySlug']
);

Related

Laravel route gets wrong controller

I'm building project with Laravel and Vue and i want my categories and tags urls to be like that:
domain.com/some-tag
domain.com/some-category
My web.php:
Route::get('/', ['uses' => '\App\Http\Controllers\IndexController#index']);
Route::get('/{category}', ['as' => 'category', 'uses' => '\App\Http\Controllers\CategoryController#index']);
Route::get('/{tag}', ['as' => 'tag', 'uses' => '\App\Http\Controllers\TagController#index']);
Route::get('/{category}/{article}', ['as' => 'category.article', 'uses' => '\App\Http\Controllers\ArticleController#index']);
I'm getting 404 error on my tags links and i know its because router matches "category" first and uses CategoryController.
What should I do? I don't want to make them unique by adding something like domain.com/tags/tag-name
I've tried to use named routes for my vue component (with Ziggy-js lib) so my link looks like
<a class="tags-block__link" :href="route('tag', {tag: tag.slug}).url()" v-for="tag in tags" :key="tag.id">
But it doesn't help
Why it should not be mixing?
You define Route::get('/{category}' and Route::get('/{tag}'. So if you open /1 in your browser it will always run the first route it is able to find that matches the pattern. So it is always running CategoryController#index yes?
Your routes should be:
Route::get('/category/{category}', ['as' => 'category', 'uses' => '\App\Http\Controllers\CategoryController#index']);
Route::get('/tag/{tag}', ['as' => 'tag', 'uses' => '\App\Http\Controllers\TagController#index']);
Read more at https://laravel.com/docs/7.x/routing
The remaining route should do fine, cause you define it last.

What does 'as' method do in Laravel

In the example from the tutorial, it shows up.
Route::group([
'prefix' => 'admin',
'as' => 'admin.'
], function () {}
Can someone tells me what 'as' does? Also, is the dot next to the 'admin' neccessary?
Thank you.
Let's say, for example, that you have this route:
Route::get('admin', [
'as' => 'admin', 'uses' => 'AdminController#index'
]);
By using as you assign custom name to your route. So now, Laravel will allow you to reference said route by using:
$route = route('admin');
So you don't have to build the URL manually over and over again in your code. You don't really need . notation if you only want to call your route admin. If you want a more detailed name of your route, lets say for ex. admin product route, then you use the . notation, like this:
Route::get('admin/product', [
'as' => 'admin.product', 'uses' => 'AdminController#showProduct'
]);
So now, you will be able to call this route by the assigned name:
$route = route('admin.product');
Update:
The previous answer I provided is valid for a single routes. For the route groups, the procedure is very similar. In the route groups you need the . notation when you add a custom name, since you will be referencing another route after that . notation. This will allow you to set a common route name prefix for all routes within the group. So by your example, lets say you have a dashboard route inside your admin route group:
Route::group(['as' => 'admin.'], function () {
Route::get('dashboard', ['as' => 'dashboard', function () {
//Some logic
}]);
});
Now, you will be able to call the dashboard route like this:
$route = route(admin.dashboard);
You can read more about this in Laravel official documentation.
you may specify an as keyword in the route group attribute array, allowing you to set a common route name prefix for all routes within the group.
For Example
Route::group(['as' => 'admin::'], function () {
// Route named "admin::"
});
UseRoute Name like {{route(admin::)}} or route('admin::')
you can use an 'as' as a named route. if you do not prefix your route name in group route than you may add custom route name like this.
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'roles'], 'roles' => ['2']], function () {
Route::post('/changeProfile', ['uses' => 'UserController#changeProfile',
'as' => 'changeProfile']);
});

Laravel 5.4 route simplification

I've been trying to find some documentation on how to accomplish the following, but it seems like maybe I'm not using the correct search terms.
I would like to implement some simplified routes in Laravel 5.4 by omitting the route name from the path – for example:
/{page} instead of /pages/{page}
/profile instead of /users/{user}/edit
/{exam}/{question} (or even /exams/{exam}/{question}) instead of /exams/{exam}/questions/{question}
Example of current routes
Route::resource('exams.questions', 'ExamQuestionController', ['only' => ['show']]);
// exams/{exam}/question/{question}
I know how to do this with route closures and one-off routes (e.g.: Route::get...) but is there a way to do this using Route::resource?
In rails the above could be accomplished with:
resources :exams, path: '', only: [:index, :show] do
resources :question, path: '', only: [:show]
end
// /:exam_id/:id
While I haven't yet found a way to accomplish my test cases using strictly Route::resource, here is what I implemented to accomplish what I was trying to do:
// For: `/{exam}/{question}`
Route::group(['as' => 'exams.', 'prefix' => '{exam}'], function() {
Route::get('{question}', [
'as' => 'question.show',
'uses' => 'QuestionController#show'
]);
});
// For: `/exams/{exam}/{question}`
Route::group(['as' => 'exams.', 'prefix' => 'exams/{exam}'], function() {
Route::get('{question}', [
'as' => 'question.show',
'uses' => 'QuestionController#show'
]);
});
// For: `/profile`
Route::get('profile', function() {
$controller = resolve('App\Http\Controllers\UserController');
return $controller->callAction('edit', $user = [ Auth::user() ]);
})->middleware('auth')->name('users.edit');
// For: `/{page}`
// --------------
// Note that the above `/profile` route must come before
// this route if using both methods as this route
// will capture `/profile` as a `{page}` otherwise
Route::get('{page}', [
'as' => 'page.show',
'uses' => 'PageController#show'
]);
No, you cannot and should not be trying to do this with Route::resource.
The whole purpose of Route::resource is that it creates the routes in a specific way that matches the common "RESTful Routing" pattern.
There is nothing wrong with wanting simpler routes (no one is forcing you to use RESTful routing), but you will need to make them yourself with Route::get, etc. as you already know.
From the documentation (not exactly your case, but related to it - showing that Route::resource is not meant to be super-configurable):
Supplementing Resource Controllers
If you need to add additional routes to a resource controller beyond the default set of resource routes, you should define those routes before your call to Route::resource; otherwise, the routes defined by the resource method may unintentionally take precedence over your supplemental routes:
Route::get('photos/popular', 'PhotoController#method');
Route::resource('photos', 'PhotoController');

Passing parameters to controller method from route in Laravel 5.5

I want to pass a parameter from my route to a controller method, to avoid duplication of code. For example, I could have my routes like this
Route::get('used-cars', array('uses' =>
'CarController#indexUsed'));
Route::get('new-cars', array('uses' =>
'CarController#indexNew'));
Route::get('new-and-used-cars', array('uses' =>
'CarController#indexNewAndUsed'));
and then have specific code for in each method for retrieving that car type. However, I would like to just have one index method in the controller, with a variable passed to it to indicate if it is a new or used car.
For example:
Route::get('used-cars', array('uses' =>
'CarController#index(1)'));
Route::get('new-cars', array('uses' =>
'CarController#index(2)'));
Route::get('new-and-used-cars', array('uses' =>
'CarController#index(3)'));
In previous versions of Laravel I believe this could be achieved using something like this
Route::get('/used-cars', array('as' => 'used-cars', function(){
return App::make('CarsController')->index(1);
}));
However I understand this was removed in Laravel 5.4. When I try it I only get class not found for the controller.
You could use a named parameter (type), and restrict that to specific values (new, used).
Route::get('{type}-cars', ['as' => 'cars.new-used', 'uses' => 'CarController#index'])->where('type', 'new|used');

LARAVEL: POST and GET routes with same name scrambling link_to_route

I got these routes:
Route::get('prefix/list/{page?}/{size?}', ['as' => 'prefix.list', 'uses' => 'MyController#getList']);
Route::post('prefix/list', ['as' => 'prefix.list', 'uses' => 'MyController#postList']);
When I call link_to_route() like so:
{{ link_to_route('prefix.list', $page, ['page' => $page, 'size' => $size]) }}
It creates this link:
http://my.site/prefix/list?page=5&size=12
But when I remove the post route, it renders correctly this:
http://my.site/prefix/list/5/12
I don't want to change the name of the routes because my system depends on them being the same. How can I solve this?
You could try just changing the order of the routes in your routes file, so that the get one comes last and overrides the post for the purposes of link_to_route().

Resources