Symfony EventSubscriber to change twig render path - php

i would ask You about proper way to change dynamicly the $twig->render path called from controller.
I want to create custom loader or event subscriber, which works like:
when i set parameter/param and call from controller
return $this->render('system/create.html.twig');
twig will check, if template in path
system/custom/variable_name/create.html.twig
exists, load it, if not, load normal path
system/create.html.twig
I can't find proper way to realize it, have not idea, what i should use, EventSubscriber(and how to Subscribe on render method), or just realize it with some custom Twig loader?
#EDIT
Problem solved! There is no way to do it "on the fly", so i extend AbstractController in custom Class, override render method and check in this method all conditions.

Related

Rendering views in other places in Yii2 - no renderPartial?

I need to render a partial view inside a custom component file in Yii2 and according to the Yii docs you can access the view instance via something like this:
\Yii::$app->view->renderFile('#app/views/site/license.php');
I went ahead and tried:
Yii::$app->view->renderPartial('//my/view/');
...but then got an error that I was trying to access a non-existent method.
I then checked out the view class and noticed it doesn't have a renderPartial and this is a method of the controller class instead.
I see it has a renderFile method and a render method; which of these should I use?
The docs don't state the render method includes the layout like the method of the same name from the controller class, so I'm not sure; as for renderFile I'm not 100% sure if that is suitable either?
Could someone explain which method would produce the same results that renderPartial produces?
You can call renderPartial from Yii::$app->controller->renderPartial('myview'); Also as you can see from source code of yii\base\Controller renderPartial calls View's render method so you can use Yii::$app->view->render. Basically there is no difference between render and renderFile, because render internally calls renderFile. But when you use render you can pass $view in several formats like path alias, absolute path whithin application or whithin module and relative path. And to renderFile you can pass only absolute file path or path alias.

How to render particular template from other module in Zend Framework

viewRenderer helper allows to render templates from the same controller's actions directory, is it possible to render template from another module?
Edit: actually I found renderBySpec method in ViewRenderer but it seems it doesn't consider module param while building path, is there any other solution besides this?
In my projects, when I need to setup a different layout than default one then in my init function of Controller I set the correct layout. Like this:
public function init()
{
$this->_helper->layout->setLayout('different-layout');
}
Hope this help you.

Action helper inclusion error + getActionController is null

I would like to write a few action helper but it seems that I am not doing it well.
First of all, in my Bootstrap file, I am doing the following:
Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH.'/controllers/helpers/My/Helper/', 'My_Helper');
In this directory, I have the following tree:
My/Helper/
- Fileupload/
- Abstract.php
- Xhr.php
- Fileupload.php
In my Controller, I successfully call the fileupload helper (in the class My_Helper_Fileupload):
$res = $this->_helper->fileupload($directory);
But in the constructor of this helper, I am trying get another helper (in the class My_Helper_Fileupload_Xhr) iwth:
Zend_Controller_Action_HelperBroker::getStaticHelper('Fileupload_Xhr');
which lead me to the error
Action Helper by name FileuploadXhr not found
What am I doing wrong? I tried a lot of stuff but I can't figure out what's wrong...
Moreover, after a few test, it seems that in my constructor of the fileupload helper, I am getting NULL when I call the getActionController method. Isn't it supposed to be set automatically?
The helper broker will remove underscores as part of its process of "normalizing" the helper name you give it. It only deals with CamelCased helper names, so your Fileupload_Xhr gets converted to FileuploadXhr.
The Zend_Loader that ends up being responsible for finding and loading the right PHP file uses underscores to determine when it should add a directory separator.
If you combine these two things, the practical upshot is that you can't have a nested folder structure under any path for the action helper broker. All the helper classes you want to be able to load for any path added with addPath must reside directly under the given path, with no intervening subfolders.
The simple solution is to move your helper from My/Helper/Fileupload/Xhr.php to My/Helper/FileuploadXhr.php.
Assuming you are using My as your appnamespace - which is what I usually do; Application is too long to type - then this is one way to do it.
In Bootstrap:
Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH .'/controllers/helpers', 'My_Controller_Helper');
Name your helper class as My_Controller_Helper_SomeHelper and store it in the file application/controllers/helpers/SomeHelper.php.
Invoke in a controller using:
$this->_helper->someHelper()
If your appnamespace is different, the just replace My_ with your appnamespace.

Chain a js action on form displayFieldError

I have a form that use displayFieldError when submitted to display errors on fields. I need to show/hide a container on certain error conditions, is possible to chain a js action on it?
Extending and redefining methods is a normal practice in Object Oriented development. Besides, you can do that globally by creating this:
class Form extends Form_Basic {
function displayFieldError(){
.. your actions ..
}
}
This will work across your project and will not loose any functionality.
all Views in Agile Toolkit carries a jquery class and is capable of calling jquery and is also chainable.
BUT the object extending the view class MUST implement an ID=NAME attribute in its HTML template.
here's an example of a view, a Text object which will not get hidden:
$t=$this->add('Text')->set('Press Buton To Hide Me');
$b=$this->add('Button');
$b->js('click',
$t->js()->hide() // hide it
)->univ()->successMessage('Hiding Text..'); // js chained
its clear that Text objects has javascript capability but will not work on itself, even attaching a click command will NOT work.
here's another example of a view, an extended HtmlElement class which WILL get hidden:
$t=$this->add('P')->set('Press Buton To Hide Me');
$b=$this->add('Button');
$b->js('click',
$t->js()->hide() // hide it
)->univ()->successMessage('Hiding Text..'); // js chained
inspect both sources and you will immediately see the difference.
therefore, make sure that the container you are referring to is a class extension of the View class and its associated Template has an id="<?$_name?>" implementation in it.
i am not sure of any other simpler way.

Eliminating the need for "controller" in the filename of a Zend Framework controller

How do I eliminate the need for the "Controller" suffix in the filename of a Zend Framework controller? It just gets tiresome to keep typing that suffix in when creating controllers, and meanwhile the file is already in a controllers folder so it's superfluous.
For instance, by default the homepage on a site goes to "controllers/IndexController.php". What if I want it to go to "controllers/Index.php"?
The latter portion of the class name is hardcoded to "Controller" in Zend_Controller_Dispatcher_Abstract::formatControllerName().
To change it, you'd have to create a custom Dispatcher class that implements Zend_Controller_Dispatcher_Interface and override the formatControllerName() function. Then assign an instance of your new Dispatcher to the front controller in your bootstrap script with $frontController->setDispatcher() before you call dispatch().
Why would you need to change the format of a controller class name anyway? It's not like that PHP file appears in a request URL anywhere.
Sounds like you're bikeshedding.

Categories