Actually I wanted to know that is there any way to define and call a function in Zend framework which does not need to put with $this like pr() function in cake php. I want to define a such a function which can be called directly in all controllers and views in zend framework. Can it be done by putting all functions in helper and use them without writing $this in front of them.
For example I wanted to make a function arrprint() to print the array in helper and use it globally with only by writing simply arrprint();
Please help me
You could just create a file anywhere and put the function in there. The just include it like you would any other files.
Or create a library called Utils and put your custom functions there as static and call them using:
Utils::myFunction();
Contrary to cake zf does not enforce much anything. You can leverage the framework to do whatever you want.
BTW $this is just a pointer to the current class and when you use $this->myFunction(); it's because that function is a member of the current class (or its parent).
Related
We need to change every <anything>/show routes to something localized.
How can we customize the show string in something like dettagli ?
You can do in two way:
By creating a custom operation, starting from the default Show operation. Copy-paste the code of the ShowOperation.php in your project, and change the route. Then throughout your project use your ShowOperation, instead of the one provided by Backpack.
By overriding the protected function setupShowRoutes($segment, $routeName, $controller) in your CrudController. If you have that method in your ProductCrudController for example, your method will be run instead of the one in the ShowOperation trait. However, this needs to be done in all CrudControllers individually, so it's less DRY.
I'm writing a MVC portlet framework and I plan to use Rythm inside my views. I would like to pass various arguments to the view and was wondering if there is a way to declare these arguments for the view using Java at runtime? I know that I can declare arguments in the view using the #args tag and that I can add custom tags from Java, but I wanted to do something similar to how ASP.NET MVC passes helper classes (HtmlHelper #Html, UrlHelper #Url, Object #Model) to the view.
If all the arguments you planned to pass to the view are global (i.e. they applied to all render session and to all templates) then you should treated them as implicit variables, meaning template author don't need to declare them but they are free to use them. Examples of implicit variables are session, request, context etc.
For how to declare implicit variables, you can refer to:
Spring-rythm
Actframework
For how to configure Rythm engine with your implicit variable, refer to
Spring-rythm
ActFramework
For how to inject implicity variables into rythm engine for each render session, you can also refer to:
Spring-rythm
Actframework
For things like HtmlHelper my recommendation is to provide reusable rythm template as tags instead of Java object. Because you need to render html snippet, thus using rythm is a natural way to go instead of let the Java code to output the content.
For things like UrlHelper if it is all about String manipulation, you can go straight with Java code, but probably the public static method is more appropriate than helper instance
I know there are view controllers and action controllers. I think that view helpers can be used from views and action helpers used from actions in controllers.
I need a class that at bootstrap or wherever, it initializes a number of configuration options, arrays for things like convert month numbers to their names and role numbers to their names.
How can this be achieved?
Put them in a model and use it anywhere you like by instantiating it and calling its helper methods. All model files are auto loaded whenever you call them.
Have a model Constants.php:
<?php
class Constants {
public static function convertMonth($month) {
doLogic();
return $something;
}
}
?>
In your controller or view:
Constants::convertMonth(12);
You could build a Resource Plugin and then add it to yout bootstrap class.
The Constants class or Resource approaches both work nicely. However, I recently had to undo/upgrade a Constants class based solution to meet new requirements, so you might want to consider your future plans before going down those paths.
Specifically, if you ever intend to support multiple languages, or even different words for the constants in different contexts, check out Zend_Translate API docs, Zend_Translate example, or this blog post.
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
I have MyForm extending Zend_Form, with init() doing regular job.
In this init() I also need to use a function to apply some transformation on some element values. Basically, get values from db -> custom transformation function -> apply them to form element.
I can put this custom function inside the form itself, but it's a general one and it will be reused by other forms.
Question: what is the best way to do this? I'm reading that helpers are associated more with views, and plugins with controllers. Is there a nice and easy way to get in the form the functionality of a custom function?
(something as helpers in symfony - just a bunch of functions)
Thanks.
p.s. this custom function is not a validator or so.