Change/Rename Controller Name In URL: Yii2 - url-rewriting

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',
]

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-

Laravel Voyager redirect to other page after login

I just started using Laravel and learning. I would like to redirect users to a custom section create through the BREAD.
In Voyager Controller, I see there is this function return Voyager::view('voyager::index');
I want to redirect to the page /admin/members
How can I achieve this? I tried to search around but can't find the solution. Hope someone can help with this. Thank you
I solved this problem by updating the user's configuration in config/voyager.php (Laravel config directory). Edit below configurations (redirect index)
'user' => [
'add_default_role_on_register' => true,
'default_role' => 'user',
'default_avatar' => 'users/default.png',
'redirect' => '/admin',
],
to
'user' => [
'add_default_role_on_register' => true,
'default_role' => 'user',
'default_avatar' => 'users/default.png',
'redirect' => '/admin/members',
],

Laravel router with additional parameters

I have controller which called by this route website.lrv/products/{category-url}
public function categoryProducts($uri, Request $request) { /** some code **/ }
I have a few links that are responsible for ordering products, i added them directly to blade template, like this:
route('client.category.products', [
'url' => Route::current()->url,
'order' => 'article',
'by' => 'desc' ])
route('client.category.products', [
'url' => Route::current()->url,
'order' => 'article',
'by' => 'asc' ])
And the request link with ordering is:
website.com/products/chargers?order=name&by=asc
Now i want to add products filters. I have many filters, each filter can contain many values. The problem is that i don't know how to make route to get some like that:
website.com/products/chargers?width=10,20,30&height=90,120,150
or something like that.
I need to get request array like that:
$request = [
....
filters => [
width => ['10','20','30'],
height => ['90','120','150'],
],
....
];`
If you need some additional info, i will edit my question.
You can access the array you are looking for by using $request->query() within the categoryProducts function.
Edit: Also worth noting you can access them from within your blade template using the Laravel helper function request(), so {{ request()->query('width') }} would return xx where website.lrv/products/category?width=xx.
Edit 2: Note that if your query string includes commas per your example width=10,20,30 the query() will just return a comma separated string, so you would need to explode(',',$request->query('width')) in order to get the array of widths
I think you have to add them manually like this:
route('client.category.products')."?order=name&by=asc";
Try this I hope it help.

Yii2 url rewrite rule using urlManages for pretty url

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){}

How to create route for static pages in laravel 4

I have a page in the database. I want to have a URL like mysite.com/page_alias for the page. How do I properly write route in laravel 4.
In kohana i did this:
Route::set('static', '<page>', array('page' => "page|page2|page3|etc"))
->defaults(array(
'action' => 'index',
'controller' => 'Static',
'directory' => 'Index',
));
Thanks.
Sorry for my english.
I never use Kohana but looking at its doc, if I'm right, this is the closest way to do what you used to do with Kohana.
Route::get('{page}', 'StaticController#index')->where('page', 'page|page2|page3|etc');
PS: take a look at this http://laravel.com/docs/4.2/routing (you probably have already checked the doc but in case..)

Resources