How to write a URL rule in Yii framework? - yii

<?php echo CHtml::link($value->title, array(Yii::app()->createUrl('forum/thread', array('id'=>$value->thread_id)))); ?>
i got a link
forum/thread/2
in my urlManager rules 'thread/<id:\d+>' => 'forum/thread',
how to change the rule and method createUrl?
createUrl('any-value/forum/thread', array('id'=>$value->thread_id))
to get in url
forum/any-value/thread/2 or forum/php-for-newbies/thread/2
I am sorry for my english, thanks a lot

URL Manager rule should look like this:
'forum/<title:\w+>/thread/<id:\d+>' => 'forum/thread', //make sure this is listed first so it has priority
'thread/<id:\d+>' => 'forum/thread',
Then in your controller you would have this:
public function actionThread($id,$title=null) {
//$title will contain title from url if sent
}

Try this:
'forum/any-value/thread/<id:\d+>' => 'any-value/forum/thread',
and with this:
createUrl('any-value/forum/thread', array('id'=>$value->thread_id))
So you should get forum/any-value/thread/2
that should work!
But If you are inside the module called forum then you would do like that:
'any-value/thread/<id:\d+>' => 'any-value/forum/thread',
and with this:
createUrl('any-value/forum/thread', array('id'=>$value->thread_id))

Related

How to define route in phalcon

I have controller named GlossaryController with 2 actions indexAction and anotherAction in view i have a directory glossary and index.volt file
i want to define a route with parameters for example
http://localhost/project/glossary/another/params it should redirect me to indexAction with parameters
In your routes.php file in app/config folder add this line:
$router->add("/glossary/another/{param1}/?{param2}", array(
"controller" => "Glossary",
"action" => "another",
));
And your anotherAction method will be like:
public function anotherAction($param1, $param2 = 0) {
//some code
}
This way first param must be sent, second one is optional, you can add this way as much params as you like.
See official docs for various ways of routing:
https://olddocs.phalconphp.com/en/3.0.3/reference/routing.html

Laravel 5.3 Route model binding with multiple parameters

Is it possible to have route model binding using multiple parameters? For example
Web Routes:
Route::get('{color}/{slug}','Products#page');
So url www.mysite.com/blue/shoe will be binded to shoe Model, which has color blue.
First of all, it would feel more natural to have a route like the following:
Route::get('{product}/{color}', 'Products#page');
and to resolve product by route binding, and just use the color parameter in the controller method directly, to fetch a list of blue shoes for example.
But let's assume that for some reason it's a requirement. I'd make your route a bit more explicit, to start with:
Route::get('{color}/{product}', 'Products#page');
Then, in the boot method of RouteServiceProvider.php, I would add something like this:
Route::bind('product', function ($slug, $route) {
$color = $route->parameter('color');
return Product::where([
'slug' => $slug,
'color' => $color,
])->first() ?? abort(404);
});
first here is important, because when resolving route models like that you effectively want to return a single model.
That's why I think it doesn't make much sense, since what you want is probably a list of products of a specific color, not just a single one.
Anyways, I ended up on this question while looking for a way to achieve what I demonstrated above, so hopefully it will help someone else.
Do not forget to declare the parameter types:
Route::delete('safedetail/{safeId}/{slug}', [
'as' => 'safedetail.delete',
'uses' => 'SafeDetailController#destroy',
])->where([
'safeId' => '[0-9]+',
'slug' => '[a-z]+',
]);
Try changing your controller to this:
class Pages extends Controller{
public function single($lang, App\Page $page){
dd($page);
}
}
You must add the Page Model.

Yii CLinkPager & urlManager rules

I have the following issue:
When I press a button in my pagination, CLinkPager generates the link as follows:
page/videos/445/?page=2
What I need is something like this:
page/videos/445/page/2
Furthermore, I need something like:
name/videos/2
where name is url of user_id = 445.
I've set some rules in urlManager but they don't quite do the job:
For step 1:
'<controller:\w+>/<action:\w+>/<id:\d+>/<page:\d+>' => '<controller>/<action>',
Any help is appreciated. Thank you in advance!
Here example for page/videos/445/page/2.This will help you understand:
//config
'rules'=>array(
'page/videos/<number:\d+>/page/<pageId:\d+>' => '/site/test',
...
//...SiteController...
//url for call www.your_site.lh/page/videos/445/page/2
public function actionTest($number, $pageId)
{
echo '<pre>';
print_r($_GET);
echo "$number<br/>";
echo "$pageId<br/>";
echo '</pre>';
die();
}
URL Management
If you want to change generation links in pager you can override CLinkPager.

Yii URL Manager: problems in create an view url

in file config main.php:
'/view-details/<slug:[a-zA-Z0-9]+>' => array('product/details', 'urlSuffix' => '.html')
create url:
$this->createUrl('product/details', array('slug'=>'my-product-demo-with-id-123'));
result:
mydomain/view-details/my-product-demo-with-id-123.html
(done! perfect generate url)
but, when visit link, this is error: unsolved request view-details/my-product-demo-with-id-123.html
if remove all character "-" (mydomain/view-details/MyProductDemoWithID123.html), it's working, not error.
what is problems? somebody can help me?
The regular expression you're using will only match single words but your url has hyphens as well.
You need to have a rule that looks like this
'/view-details/<slug:[-\w]+>' => array('product/details', 'urlSuffix' => '.html')
I'm not sure (new at Yii!) but probably this can be related to a bad method in your Controller actionView().
Look if you have in your controller "View-details" a method similiar to "actionMyProductDemoWithID123()" and inside of this, you render that file.
Example: yourDomain/yourController/yourView
Class YourController{
actionYourView(){
$this->render('yourView');
}
}
In your case, you probably have something similar to this:
Class view-details{
actionMyProductDemoWithId123(){
$this->render('MyProductDemoWithID123');
}
}
If it's not, you can take a look at this post: http://www.yiiframework.com/wiki/404/hyphenation-of-routes-in-url-management/
You missed the "-" part of your regex so it won't match try
'/view-details/<slug:[a-zA-Z0-9\-]+>' => array('product/details', 'urlSuffix' => '.html')
In your regex you are allowing only alphanumeric values. Your have to allow "-" also as you are using in your parameter(slug).
Regex with Alphanumeric and -,_
'view-details/<slug:[a-zA-Z0-9-_]+>' => array('product/details','urlSuffix' => '.html'),

Yii Controller Parameters

When I create this method
actionTest($parameter){
print($parameter);
}
inside of a yii controller and attempt to access it in the browser I get an error 400.
How can I set up yii so that if I enter:
/controller/test/text it would simply print the string 'text' on the screen instead of returning an invalid request error?
I already verified that the URL is correct. If I write
actionTest(){
print('text');
}
and then go to /controller/test/text then it works just fine.
How can I set up yii so that a controller action can accept parameter values in the URL?
You have to edit the urlManager rewrite rules array in your config.php to include
'urlManager'=>array(
....
'rules'=>array(
'controller/test/<parameter:\w+>' => 'controller/test',
...
),
),
with your controller function as
actionTest($parameter){
print($parameter);
}
#curtis: Thanks. That
helped!!! <controller:\w+>/<action:\w+>/<id:\w+>'=>'<controller>/<action>
– Curtis Colly Mar 31 '13 at 3:27
The rule <controller:\w+>/<action:\w+>/<id:\w+>'=>'<controller>/<action> line will send the last parameter to the controller as $id if you want to use $parameter you should change id to parameter.
<controller:\w+>/<action:\w+>/<parameter:\w+>'=>'<controller>/<action>
In the controller you can also type
actionTest(){
print($_GET['parameter']); //or $_REQUEST
}
Try the url: /controller/test/parameter/text , it should work.
Suggested url rule by #topher is also correct.