Cannot set form action in a widget

I’ve module called User which contains widget in its components folder. The widget extends CActiveForm and used to display a login block in a sidebar on every page of site. Login action is handled by a UserController which resides in User module.

I set action element of my widget like this:




public function init()

{

    $this->action = array('user/user/login');

}



This works on all pages EXCEPT those which handled by User module, e.g. in ‘user/user/login’ page I have my form action ‘user/user/user/login’, because module name is also prepended.

[list=1]

[*]How should I avoid this problem?

[*]Is it appropriate to use Widget to display a login block?

[/list]

Add a slash at the beginning:




public function init()

{

    $this->action = array('/user/user/login');

}

 

Thanks! I didn’t find this in docs. What about second question?

Alternative to adding a slash would be to make use of the createUrl() method in the web app or the controller. Using a Widget to display a login box is appropriate, it’s either that or a partial at the end of the day.

About the second question, yes, is appropriate and suggested if you want to display in each page.

There is a wiki about it, I think.

Thanks for replies. By the way, what is the difference between ‘action/controller’ and ‘/action/controller’? When should I use “slashed” version? I read the sources, but wasn’t able to figure it out.

The slash means that this is an "absolute route".

If in a controller you use ‘action’, Yii will create the url ‘controllerId/action’ (or ‘module/controllerId/action’ if you are in a module).

If you use ‘controller/action’, it will be ‘controller/action’ or ‘module/controller/action’ if you are in a module.

Using the slash means that your route is absolute, and nothing has to be prepended to your route.

So if you use the route ‘/user’, where ever you are it will be considered the route /user, if you are in module or controller as well.

In this case you need the slash, because otherways in a module the module id will be prepended, with the result you experimented.