I want to write rule for below url but i am unable to write.
I have tried this but it is wrong
'member/find/<SearchForm[city]>/<SearchForm[location]>' => 'member/find'
How should i write rule for below url?
member/find?SearchForm[city]=dilli&SearchForm[location]=chandnichowk
So if "member" here is the name of your controller and "find" is the name of the action you should put something such as:
['class' => 'yii\rest\UrlRule',
'controller' => ['member'=> 'member'],
'extraPatterns' => [
'GET find/<city>/<location>' => 'search',
]
],
this should map to
public function actionFind($city,$location){}
Related
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-
When grouping routes through resources as such:
Route::resource('books/{book}/catalog', 'CatalogController', ['names' => [
'index' => 'catalog.index',
'store' => 'catalog.store',
'update' => 'catalog.update',
'destroy' => 'catalog.destroy',
], 'except' => ['create', 'edit', 'show']]);
The route:list command outputs:
DELETE | api/v1/books/{book}/catalog/{catalog}
PUT|PATCH | api/v1/books/{book}/catalog/{catalog}
However I was hoping for:
DELETE | api/v1/books/{book}/catalog
PUT|PATCH | api/v1/books/{book}/catalog
Any suggestions how I can get the results when grouping, without the extra {catalog} parameter in the DELETE and PUT|PATCH routes?
Why are you not just using the apiResource method?
It makes it all so much tidier.
Route::apiResource('catalog', 'CatalogController');
I am assuming that there is a one-to-one relationship between books and catalogs, and that is why you don't need the catalog parameter to determine which catalog to delete/update.
If this is the case, what you're looking for is singular resource routing. Laravel does not provide this by default.
I have created a package that adds this functionality to Laravel: shiftonelabs/laravel-singular-resource-routes.
One change you would need to make, however, is to change your index route to the show route. Singular resources are by definition singular, so there isn't a group of resources to index, there is only one resource to show.
After installing the package, you would update your route to include the 'singular' => true option (and change your index route):
Route::resource('books/{book}/catalog', 'CatalogController', [
'names' => [
'show' => 'catalog.show',
'store' => 'catalog.store',
'update' => 'catalog.update',
'destroy' => 'catalog.destroy',
],
'except' => ['create', 'edit'],
'singular' => true,
]);
I was looking to change controller name in URL. Which, we can do by renaming the controller name in module. But, Through URL manager if we can do it. It will be better.
Module: user,
Controller: api,
Action: index
Right now,
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:(api)>/<action:\w+>/<id:[a-z0-9]+>' => 'user/<controller>/<action>',
'<controller:(api)>/<action>' => 'user/<controller>/<action>',
]
];
And, I can access it through.
http://dev.example.com/api/index
But, I was looking to change it to
http://dev.example.com/world/index
How can I do it? Any help/hint/suggestion is appreciable.
You can create custom url rules by adding items to the rules array.
So, in your case insert this into the rules array
'world/index' => 'api/index'
You can read more about URL rules here.
also you use ControllerMap
it useful when you are using third-party controllers and you do not have control over their class names.
below code in component in main.php in advance or web.php in basic
for example:
'controllerMap' => [
'api' => 'app\controllers\WorldController',
]
i have in my controller
public function details($id)
{
$claim = Claim::findOrFail($id);
$details = $claim->details;
return response()->json([], 200);
}
and I have in my routes
Route::resource('claims', 'Admin\\ClaimsController',['names'=> ['details'=>'admin.claims.details'], 'only' => ['index','store','update','destroy','details']]);
when I run php artisan route:list i do not see the admin.claims.details( admin/claims/1/details) in the list
the documentation is pretty vague here so I'm asking how to properly set a custom route? How do I specify if its "POST" or "GET"?
To override the default resource controller actions' route names, you can pass a names array with your options.
For example:
Route::resource('claims', 'ControllerClassName', [
'names' => [
'index' => 'admin.claims.details',
'create' => 'admin.claims.create',
// etc...
],
'only' => [
'index','store','update','destroy','details'
]
]);
REF: https://laravel.com/docs/5.2/controllers#restful-naming-resource-routes
Here are examples of setting custom named get/post routes.
GET Route
Route::get('claims', ['as' => 'admin.claims.details', uses => 'ControllerClassName']);
POST Route
Route::post('claims', ['as' => 'admin.claims.details', uses => 'ControllerClassName']);
REF: https://laravel.com/docs/5.2/routing#named-routes
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