my routing is not working proper in laravel - laravel

Here are my two routes mentioned in http/routes.php
Route::get('/{buy_type}-property/{type}-in-{city}/{location}/project/{projname}/{section}', 'APP\DetectHookController#detectProjectcase4')->where('projname', '[A-Za-z0-9_\-A-Za-z0-9_\-]+')->where('location','[A-Za-z0-9_\-A-Za-z0-9_\-]+')->where('section', '[A-Za-z0-9_\-A-Za-z0-9_\-]+');
And second one is
Route::get('/{buy_type}-property/{type}-in-{city}/{location}/project/{clustername}/{projname}', array( 'as' => 'project-with-cluster', 'uses' => 'APP\DetectHookController#detectProjectcase2'))->where('projname', '[A-Za-z0-9_\-A-Za-z0-9_\-]+')->where('location','[A-Za-z0-9_\-A-Za-z0-9_\-]+');
I want conditional routes based on the {section} parameter in first route.
The second one doesn't get call when it is supposed to be called as both routes are having same parameters. Can someone suggest me as I am helpless for almost a week.

You can re-structure your route as:
Route::get('/{buy_type}-property/{type}-in-{city}/{location}/project/{projname}/cluster/{clustername}', array( 'as' => 'project-with-cluster', 'uses' => 'APP\DetectHookController#detectProjectcase2'))->where('projname', '[A-Za-z0-9_\-A-Za-z0-9_\-]+')->where('location','[A-Za-z0-9_\-A-Za-z0-9_\-]+');

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 custom Route

Hi I want to use routes like this for a page
Route::get('{slug?}-{newsSlug?}-{id?}', array('as' => 'subpages', 'uses' => 'HomeController#subpages' ));
It does not work like this
Route::get('{slug?}/{newsSlug?}/{id?}', array('as' => 'subpages', 'uses' => 'HomeController#subpages' ));
This way the other pages do not work naturally.
Could you suggest me a solution about this.
Where you define a route like this
Route::get('{slug?}-{newsSlug?}-{id?}'...)
without a constant, it means it will cover most cases of routing, which is why other pages do not work.
For them to work, you need to put that code at the end of your routes file, so that other routes will have precedence.

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().

How to display two dynamic names within one url using laravel

The following code:
{{url('/'.$subjecttype->name)}}
is the name 'garden' wrapped up in a url. This gives me localhost/garden with obviously garden as the dynamic name. With my routes setup like so:
Route::get('/{subject}/', array( 'as' => 'subject', 'uses' =>
'SubjectController#getsubject'));
The question is how would I setup two dynamic names within one route? For example
localhost/garden/12
so i would want my route to look something like this
Route::get('/{subject}/{id}/', array( 'as' => 'subjectid', 'uses' => 'SubjectController#getsubjectid'));
but more importantly what would it look like in my view? so that I have the of my garden header wrapped up in a url that looks like this:
'gardening tips for beginners' which is {{$subjecttype->title}}
below is my very poor attempt at what i want but i hope you get the picture.
{{url('/$subjecttype->name/$subjecttype->id/'.$subjecttype->title)}}
Thanks
For your route:
Route::get(
'/{subject}/{id}/',
array(
'as' => 'subjectid',
'uses' => 'SubjectController#getsubjectid'
)
);
you can generate the URL with the following code:
$url = URL::route(
'subjectid',
array(
'subject' => $subjecttype->name,
'id' => $subjecttype->id
)
);
or, if you prefer to use the helper functions:
$url = route(
'subjectid',
array(
'subject' => $subjecttype->name,
'id' => $subjecttype->id
)
);
That's going to give you a URL like http://example.com/subjectname/42. If you want to add another parameter like the title at the end of the URL, you'll need to add another parameter to your route definition. If you don't you're going to get 404 errors because no route will match the URL.
For the second part of my question using the 'gardening tips for beginners' example:
gardening tips for beginners

Resources