CodeIgniter helper inside controllers - forms

can i call helper functions inside controller classes?
let's say i have this controller with the _open_form method
class User extends Controller {
function _open_form($action){
print_r(form_open($action));
}
}
i tried echoing out the result of form_open()
but it returns null. it seems that helper functions can't be called inside controllers
if your wondering why i need to use it inside the controller instead in the view
because we are required to use the given template parser xD

lolololol
i figured it out. it seems that the view file did not escaped the result of form_open()
try using htmlentities(form_open($action));
it should escape < and > symbols
lol sorry for the stupid question :))

Did you load the helper file?
$this->load->helper('form');

Yes you just need to load it inside your function:
$this->load->helper('form');
More details here http://codeigniter.com/user_guide/general/helpers.html

Related

How to call a Mojolicious controller method after failing on template

I have a Mojolicious problem that I suspect has an easy solution, but I can't swim through all of its code.
$r->get(
'/:controller/:action',
sub {
my $c = shift;
$c->render_maybe && return;
# No template. Either call controller->action() or dispatch as POST
}
);
$r->post('/:controller/:action');
As you can see, I have two routes that use the same URL, one for GET and one for POST. The POST is straightforward. It renders after finding the controller and action method and isn't concerned with a template. I have the GET method working where a template exists, ignoring the controller, by using a callback with render_maybe(). My issue is, if there isn't a template, I want to go ahead and run the controller's method.
One solution would be to simply identify my controller and call the action. Since I'm using placeholders in my route, I can't simply hard code this. So, is there a Mojolicious way of getting my controller class, or actual code that will get the class and call the method? I have both controller and action defined in stash, so this really shouldn't be a big deal. Mojo knows how to do this internally.
Another option would be to convert this to a POST method and run it as normal. I don't know if it's best to either come up with the URL, or find the defined POST route, or just convert my GET to a POST. I'm not sure how to accomplish any of these.
Thanks.
I'm not sure if this is the intended use case for under(), but it seems to be a decent solution to my problem.
$r->under( '/:controller/:action', sub { !shift->render_maybe } )->get('/');
$r->post('/:controller/:action');
Chaining under()->get() will create a nested route. The first in the stack will render a template, if it exists, from the render_maybe, stopping there. If the template doesn't exist, it will go to the standard get(), which will first check for a controller action. This is exactly what I was wanting.

Having difficulties extending Zend_Form

Currently I have big difficulties extending Zend_Form.
I have the basic class called Forms_LpaManageEmailForm.
It is used separately and works fine.
Next I've created a new class form
called Default_Form_CartReport witch extends Forms_LpaManageEmailForm.
So the task is to render Default_Form_CartReport and slitely modificate it.
In other words I need all functionality of
Forms_LpaManageEmailForm class but with overriden _addMultiOptionsForMultiSelect() function
(what is done) and changed button label (doesn't solved).
In basic class I have hidden element named id which value is filled with
$this->_entry_id['entry_id']. When I use basic form separately - its woks fine. But
when I run extended form(Forms_LpaManageEmailForm) I see that hidden id element's value is empty. In basic class in construct section I run
Zend debugger(with this line Zend_Debug::dump($this->_entry_id['entry_id'])) to see if the
value is passed. And it's passed :) When I repeat this in init() section it shows NULL...
As I barely understand - the problem lays in init() functions, in the way it is called.
I think something is wrong with Default_Form_CartReport class skeleton.
I've uploaded code to: PASTEBIN
Really need help in this question.
Thank you!
I believe your issues are causing my the fact that Forms_LpaManageEmailForm:: __construct is calling $this->init() directly. if you open the Zend_Form, you will notice that the __construct is also calling the $this->init() function. This cause your init() function to executed twice.
Try to load all your logic & elements solely in the __construct function, and don't use the init() function. also, the __construct function in each form class should always call the parent::__construct before any additional logic.

In sinatra if I want add a method to String class, where should I put these code?

class String
def show(word)
word
end
end
if I want to add this method in Sinatra, I first try to add to helper code, but it gave me a no method error, what is the best way to do this
You shouldn't add it inside any helpers as helpers ... do is a method in itself.
What you should do is create an ext directory in your project root, add a file called string.rb and put your code inside that.
Then in your app.rb, add require './ext/string.rb'
This would be a simplistic way to go about it, but there are many other ways to structure your code.
EDIT:
As per Matt's response below, you should call your directory core_ext instead of ext

zend, how can i get data to layouts or views without assigning it everytime

i want to be able to get data from my controllers to be made accessible from all views or layouts.. so that i dont have to assign a variable to each controller everytime..
in my case, in my layout.phtml, theres a list of categories direct from the db, for now, i am assigning this to every controller:
class productsController extends Zend_Controller_Action {
public function init() {
Zend_Layout::getMvcInstance()->assign('categories',$dbArrayCategories));
}
}
but i dont want to repeat it every time for each controller..
This sounds like a case where I would use a ViewHelper: http://framework.zend.com/manual/en/zend.view.helpers.html. This will let you use it in any view or layout. I typically pass a model into my helper, but you can definitely have it load one up by default.
You might extend Zend_Controller_Action with you own base class and assign the values directly in the constructor or init method.

Is it possible to route my simple URL (without specifing the module, controller and action) in Zend framework?

I am using zend framework for my project and I have a requirement to route the path where I want it. For example:
I have a path www.example.com/module/controller/action1 and I want to internally route in to www.example.com/module/controller/action2.
But the main problem is I do not want to use the function in which I have to specify the module, controller and action [$this->_forward('action2', 'controller', 'module');], simply I something like this: $this->_forward('module/controller/action2');.
Please suggest me if anybody has its solution. its urgent need of my project.
Thanks,
Jitu
The controller method _forward() can accept only the action if it is in the same controller and module as the first action you are forwarding from.
So in your action1Action() method, you can simply call:
$this->_forward('action2');
If your second action is not in the same module/controller, you could subclass Zend_Controller_Action into your own "base" application controller that all your other action controllers inherit from (a good practice on ZF projects I find) and then create another one called _forwardFromUrl() or something like that, which breaks your URL apart and passes it to _forward() (or create an action controller helper if you just need this one extra thing).
This example is simplified and assumes your $url will always be in the format module/controller/action:
protected function _forwardFromUrl($url)
{
$parts = array_reverse(explode("/",$url));
$this->_forward($parts[0],$parts[1],$parts[2]);
}
Hope that helps!