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.
Related
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
I want to render page name through URL request parameter.
like -
r=page/view&name=page_name
instead of id - r=page/view&id=1
please i need to know what i can do in
actionview(), viewfile and urlmanager (code generated by gii)
Help to resolve my issue.
You can do like this -
your request url - r=page/view&name=page
In your action controller -
public function actionView(){
if(isset($_REQUEST['name']) && !empty($_REQUEST['name'])){
$page_name = $_REQUEST['name'];
$this->render($page_name);
}
}
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'),
<?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))
I have app in Yii and i extend all classes from some base controller and i have these code in it :
protected function beforeAction($action)
{
$this->setglobalvariable();
return parent::beforeAction($action);
}
as i just understand , these code prevent the Captcha to show , because when i delete it , the captcha shows up ! the captcha function is :
public function actions()
{
return array(
// captcha action renders the CAPTCHA image displayed on the contact page
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFFFFFF,
'minLength'=>2,
'maxLength'=>3,
'width'=>60,
),
// page action renders "static" pages stored under 'protected/views/site/pages'
// They can be accessed via: index.php?r=site/page&view=FileName
'page'=>array(
'class'=>'CViewAction',
),
);
}
So how could i use beforeAction and captcha in same time ?
The confilict is in your structure , Show us more code . put the program in fresh yii and test it.
beforeAction function , Do not have any conflict with other Yii methods or functions.
The probelm is in your code.
Obviously there is some code in your Controller::setglobalvariables() method that conflicts with the captcha's code.
The CCaptachAction::run() method uses $_GET parameters. Are you somehow resetting $_GET ?
Can you show us the code ?