How to display two dynamic names within one url using laravel - 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

Related

How to start using Slug URLs without breaking old in Yii2

i want to rewrite my URLs from
/view?id=100
to
/view/100-article-title
But the site already has several thousand search pages. As far as I know, such a change can be bad for SEO. Is there a way to keep the old URLs working when switching to the new routing.
*I am creating links as follows:
Url::to(['view', 'id' => $item->id])
Is it possible to create links further through ID?
you can create getLink() function on your model and use it on. Then, when function runs you can check id if id <= 100 then return Url::to(['view', 'id' => $item->id]) else return Url::to(['view', 'id' => $item->id, 'slug' => $this->slug])
And add route with slug on your main.php
Look at my example.
First config urlManager in config.php in 'app/config' folder. In my case look like:
'components' => [
...
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
''=>'home/index',
'<slugm>-<id:\d+>-' => 'home/view',
],
],
.
...
],
and create link as fallow:
Url::to(['/home/view', 'id' => $model->home_id, 'slugm' =>$model->title_sr_latn])
and finaly url look like:
https://primer.izrada-sajta.rs/usluge-izrade-sajtova-1-

my routing is not working proper in 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_\-]+');

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

Get a parameter from a route?

I have the following route:
Route::get('news', [
'as' => 'news',
'uses' => 'ArticleController#getIndex',
]);
There are also other routes that use the same resource controller, e.g:
Route::get('travel', [
'as' => 'travel',
'uses' => 'ArticleController#getIndex',
]);
In the article controller, how can I get the category of the articles. e.g. travel or news?
I can't turn it into a route param e.g.
Route::get('{travel}', [
'as' => 'travel',
'uses' => 'ArticleController#getIndex',
]);
As there are other sections such as contact, faqs, etc that do not use the article controller.
I know I could put it all on one route:
Route::get('/article/{category}', [
'as' => 'travel',
'uses' => 'ArticleController#getIndex',
]);
But I want pretty URLS like:
mydomain.com/category-slug/article-slug
Ideally if I could use a resource controller:
Route::resource('news', ArticleController');
With some sort of array:
Route::resource(['news', 'travel'], ArticleController');
So my questions:
How can I get the name of the resource. e.g. news, travel in my controller?
Is there an easy way to specify varying routes to the same resource?
For Example Keep your routes like this as your static routes will be limited and need to be unique so they will not get conflict between routes
demo routes :
Route::get('contact', [
'as' => 'contact',
'uses' => 'contactController#getIndex',
]);
Route::get('{travel}', [
'as' => 'travel',
'uses' => 'ArticleController#getIndex',
]);
In order to achieve a route like that:
mydomain.com/category-slug/article-slug
Use the following:
Route::get('/{category}/{article}', 'ArticleController#getArticle');
Then in your controller, you will have 2 parameters in your getArticle method.
public function getArticle($category, $article)
{
//Your code here
}
In order to have pretty urls you need to make a few steps:
1)
First of all you need to add a column to your articles table called for example 'slug'.
2) Then you need to specify your route key in your article model. Like this:
Article model
public function getRouteKeyName()
{
return 'slug';
}
3) When you add an article you should create the slug by your own in order to be unique. so in your controller that you create the article add this code.
$slug = str_slug($article['title'], '-');
$slugs_count = DB::table('articles')->where('name',$article['title'])- >count();
if($slugs_count>1){
$slug = $slug.'-'.($slugs_count-1);
}
$article->slug = $slug;
$article->update();
4) Now we have to set the route
Route::get('/article/{slug}', [
'as' => 'travel',
'uses' => 'ArticleController#getIndex'
])->where(['slug','[\w\d\-\_]+']);
5) Finally in order to get the article you need
$article = Article::where('slug','=',$slug)->first();
If you want to use categories etc you could pass more parameters in your route and manipulate it in your controller. like this
Route::get('/article/{category}/{slug}', [
'as' => 'travel',
'uses' => 'ArticleController#getIndex'
])->where(['slug','[\w\d\-\_]+','category','[\w\d\-\_]+']);
The where clause is to restrict the parameter with some regular expressions(its optional).
I forgot to mention that the routekeyname in your model works in laravel 5.2.

How to combine these two routes?

I have these two routes:
Route::post('/post',
array(
'uses' => 'PostController#newPost'
)
);
Route::post('/post/picture',
array(
'uses' => 'PostController#newPost'
)
);
In the actual controller, I differentiate between the two parameters (since they use the same controller), but how can I combine the two routes above?
Give the following a try
Route::post('/post/{picture?}',
array(
'uses' => 'PostController#newPost'
)
);
Wrapping picture in the {} brackets treats that URL segment as a parameter. Using a ? at the end of the parameter treats it as optional. One caveat: This will also catch things like
/post/some-other-thing
If you're concerned about catching the other items, give the following a try
Route::post('/post/{myvar?}',
array(
'uses' => 'PostController#newPost'
)
)->where('myvar','picture');;
The second parameter to the where method can be any PCRE regular expression.

Resources