invoking an action helper - php

I have a directory structure such that I have three directories inside my root directory, namely application, public and library.
Now, inside the library directory, I made a directory Custom, inside which I have a directory Controller, inside which I have a directory Action, inside which I have a directory Helper, and this directory contains a php file named 'LinkTo.php'. Inside this file, I have a class named Custom_Controller_Action_Helper_LinkTo which extends Zend_controller_Action_Helper and provides with a simple function called linkTo($inputString)..which outputs the url as per the input string parameter. But, I get this error "Action Helper by name CustomControllerActionHelperLinkTo not found " even though I have mentioned 'Custom_' in autoload namespaces in my application.ini, and have also taken care of include paths in my index.php.
Please help! How does one make an action helper like that and invoke it?

Did you specify path for the custom Action Helpers ?
You can do this in your application.ini, add following line:
resources.frontController.actionHelperPaths.Custom_Controller_Action_Helper_ = "Custom/Controller/Action/Helper"
After you specified path for your custom helpers, you need to initialize them for the later use. This can be done in Bootstrap:
protected function _initHelpers()
{
Zend_Controller_Action_HelperBroker::addHelper(new Custom_Controller_Action_H‌​elper_LinkTo());
}
If you want to use helper as a method of the helper broker, for instance:
$this->_helper->LinkTo(); your custom helper should implement direct() method.

Related

How to create and load new helper in CodeIgniter 4

i want to create new function in helper, but it still failed :
Call to undefined function
i save my helper at app/Helper/Text_helper.php using namespace App\Helpers;
and load helpers on BaseController using protected $helpers = ['text'];
Reference : https://codeigniter4.github.io/userguide/general/helpers.html#extending-helpers
but it's still not working
It's not mentioned in documents but remember to add a suffix _helper to filename of your helper otherwise it will not work in codeigniter 4.
For example if you have created a helper xxx.php, change it to xxx_helper.php.
To load a helper you can use helper function (Like: helper('xxx.php');) or add it to $helpers array that is an protected property in BaseController
If your idea is to "extend" (replace) a function on the stystem/helpers/text_helper note the lowercase in the name of the file, you have to respect it.
Also, the helper doesn't need a namespace... the helper loader will search for it.
The helper() method will scan through all PSR-4 namespaces defined in app/Config/Autoload.php and load in ALL matching helpers of the same name. This allows any module’s helpers to be loaded, as well as any helpers you’ve created specifically for this application. The load order is as follows:
app/Helpers - Files loaded here are always loaded first.
{namespace}/Helpers - All namespaces are looped through in the order they are defined.
system/Helpers - The base file is loaded last
the namespace will be used to load a helper on other location, for example:
helper('Libraries\MyFunctions');
as long as that path can be found through a namespace that has been set up within the PSR-4
Reference:
https://codeigniter4.github.io/userguide/general/helpers.html#extending-helpers
You need to load the helper into the app/Config/Autoload.php and still not work then please try to run composer dump-autoload

ZF2 change extends class

The answer to my question ZF2 FormInput to show error class on validation fail is to create my own form view helper, overriding the render function. While this works beautifully for elements being rendered using forminput, it doesn't help on elements that inherit from forminput. For example, FormCheckbox inherits from forminput but not MY forminput:
<?php
namespace Zend\Form\View\Helper;
//...
class FormCheckbox extends FormInput {
//...
}
In this case I would need to create ANOTHER form view helper for formcheckbox exclusively to extend MY forminput. And again for any other view helpers I want to include (formdate, formemail, formpassword, etc).
Instead of creating multiple view helpers is it possible to create a single view helper and tell ZF2 to use that in all calls to the original view helper when made by a ZF2 view helper?
i.e. \Zend\Form\View\Helper\FormCheckbox would extend \RPK\Form\View\Helper\FormInput, which would extend \Zend\Form\View\Helper\FormInput.
A solution I can think of, is rewriting a part of the autoloader.
In the autoload function, you add an extra check to check if the requested class is \Zend\Form\View\Helper\FormInput, and if that's the case, you load your custom FormInput.
In your own FormInput you don't extend the Zend FormInput, but you create a copy of it, and modify the parts that are needed to be modified.
Unless ZF2 is using dependency injection for it's form helpers, this is the only way I can think of (without altering the base library code).
What I have used are view partial. With partials you can define your own html for every input-element and you have access to all attributes and messages (e.g error-messages) of the input-element:
in your partial phtml:
<?php
$options = $element->getOptions();
$t = $this->t();
$value = $element->getValue();
$messages = $element->getMessages();
$attr = $element->getAttributes();
you can use a partial like this:
$this->formRow($form->get('myelement'), null, null, 'mypartial');
I am using composer to install ZF2. As composer is generating my autoload functions I can specify an autoload path in the composer.json file:
"autoload": {
"psr-4": {
"Zend\\Form\\View\\Helper\\": "vendor/rpk/Rpk/Form/View/Helper"
}
},
This will search in my vendor folder for the class before looking in the zend folder.
This does not let me extend the FormInput, but calls my FormInput in its place. Calling extends on my FormInput puts the app into an infinite loop, so we need to copy the contents of FormInput into my class and make the changes there.

Yii2 vendor url for registerCssFile

I'm try to make a Yii2 extension and just want to register a css file from extension folder.
Css file directory is:project_folder\vendor\extension-ven\extension-name\assets\css\main.css
I must be use registerCssFile function.
public void registerCssFile ( $url, $options = [], $key = null )
What am i must be write to url in registerCssFile function. I can't use alias Yii::getAlias('#vendor/extension-ven/extension-name/assets/css/main.css');
Is there any method am i use like alias but give me a vendor url?
Thanks
The recommended way to do it is:
Prepare Asset class with the js and css files for the extension. Example here.
Inside add sourcePath property pointing the assets folder (remove basePath and baseUrl properties).
public $sourcePath = '#vendor/extension-ven/extension-name/assets';
In your extension class register your Asset class (i.e. if this is Widget you can do it in the run() method; this assumes your extension implements ViewContextInterface interface like Widget class).
YourAssetName::register($this->view);
All the asset files are automatically copied from the source folder to the assets folder in the public web folder when extension is called.

Zend Framework 1.10.7 telephone validator

Please See Correct Answer for solution to the requested question.
Hi,
Recently I have been searching for telephone validation in zend framework which I think is a missing component of their Validator framework. Therefore I created custom telephone validator which I would like to share with you.
Put code below in a file accessible by require_once php statement. Here we suppose that this code is pasted in file telephoneValidator.php.
class Custom_Validator_Telephone extends Zend_Validate_Abstract
{
const INVALID = 'This field is required';
protected $_messageTemplates = array(
self::INVALID => "Incorrect telephone number"
);
public function __construct()
{
}
public function isValid($value)
{
if(preg_match("/^(\+)?(\([0-9]+\)\-?\s?)*([0-9]+\-[0-9]+)*([0-9]+)*$/", trim($value)))
{
return true;
}
else
{
$this->_error(self::INVALID);
return false;
}
}
}
How to Use it: Put $tel Zend_Element below in your Zend_Form object with addElement method
require_once("telephoneValidator.php")
$tel = new Zend_Form_Element_Text($fieldName);
$telValidator = new Custom_Validator_Telephone();
$tel->addValidator($telValidator, true)
->setAllowEmpty(false)
->addValidator('NotEmpty', true, array('messages' => array(
'isEmpty' => $label.' is required')))
->setLabel("Telephone Number");
$form->addElement($tel);
Error message from this validator can be modified using setMessage method of Zend_Validate_Abstract class
$telValidator->setMessage("%value% is not correct telephone number");
$tel->addValidator($telValidator, true)
This validator is working fine with phone numbers in following format
+(92) 345-5141637
+(92)-345-5141637
(92) 345-5141637
(92)-345-5141637
+(92)-345-5141637
92-345-5141637
+92-345-5141637
+923455141637
923455141637
(92)-(345)-5141637
I have'nt put length check yet on phone number but it will require to create a filter for filtering digits from the input telephone phone number then using StringLength
validator.
Although I am new in Zend framework, I would like to know that how can I automatically include my classes in custom folders inside application folder using autoloader of Zend framework. For example I have my custom classes in MajorClasses folder inside application folder, please tell me the way to automatically include all the classes inside my MajorClasses folder just be specifying its name because there can be many files inside that folder but I want them to be included automatically. Is this possible in Zend framework?
Why did you post your full telphone stuff? Your question is just how do you enable autoload of custom files in Zend? Right?
In Zend 1.10.7 you can add the following to public/index.php ABOVE your bootstrap->run command
require_once "Zend/Loader/Autoloader.php";
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('Custom');
You can register as many custom namespaces as you like. In this case Custom is a new namespace thus your classes should be named as follows.
class Custom_Validator_Telephone extends Zend_Validate_Abstract
Now about your directory structure, first question your MajorClasses folder is inside application/??? if so ok, in the same file, as above, there should be a set_include_path() function being run. Within it your setting your library path, now we can add the path to your new directory.
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
APPLICATION_PATH.'/MajorClasses'.PATH_SEPARATOR,
)));
WITHIN MajorClasses folder you will NOW have to create a directory FOR EACH namespace. So if you have the namespace Custom, you create the directory, also you have to create the Validator directory since you're naming it like that, so your path would be.
application/MajorClasses/Custom/Validator/Telephone.php
Telephone.php should be the name of your class file, the class filename is always the last namespace in the classname.
Did I miss anything?
This question comes under Zend Resource auotloading http://framework.zend.com/manual/en/zend.loader.autoloader-resource.html
In short, in order to include all files under particular folder we need to follow following rules.
1) Suppose all files under MajorClasses folder are started by Custom i.e. class Custom_validator_Telephone, so our namespace for this folder is Custom. In order to include files under this folder we need to create an instance of zend resource autoloader
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => "/path/to/MajorClasses",
'namespace' => 'Custom'
));
2) Now we have our resource autoloader ready, we need to add resources to this object for example if I have folder with name validators inside MajorClasses folder and all files inside of this folder are prefixed by Custom_Validator then namespace of this folder is Validator because we have already defined Custom as prefix of the parent resource object.
$resourceLoader->addResourceType('validator', 'validators/', 'Validator');
Here
1st parameter says about name of resource we are adding and is used for internal recognition.
2nd parameter defines path of the folder relative to the base Path we declared when instantiating resource autoloader object, so path of this resource is /path/to/MajorClasses/validators/.
3rd parameter specifies namespace of the class i.e. it will be concatenated by the resource object's namespace(in our case it is Custom) so prefix of complete class upto this point is Custom_Validator and php file inside this folder will be postfixed with this class name after stripping .php file extension
3) Now we can put Telephone.php inside validators folder and if we place above code in bootstrap's any function for e.g. _initPlaceHolders then we can create instance of Custom_Validator_Telephone anywhere in our application without need of using require_once statement.
$telValidator = new Custom_Validator_Telephone();

Change the .phtml extension in Zend Framework (Module Only)

The standard extension for template files in Zend Framework is .phtml... I need to change them to .js in one specific module... can anyone help... I'd ideally like to change this a Controller level...
Many thanks...
In your controller:
public function init() {
$this->getHelper('viewRenderer')->setViewSuffix('js');
}
If you need to apply this to all controllers within a module, you should place this in an abstract controller class used for that module and have each controller in that module inherit from that abstract class.
You could theoretically put this in the module's bootstrap, but it would set the view suffix to 'js' for every request, even ones that end up not being routed to that specific module. This is because every module's bootstrap is executed for each request, regardless of which module is selected by the dispatcher.
The controller's init() function, though, will only execute when the module is selected for dispatch.

Categories