I have created my own view helper according to this example. The view helper calls my model to create a form. The view Helper returns a form object.
My question is wether it is possible to assign a custom view script to the View Helper for the form. Or should i just use a partial for the script?
Thanks very much
View helpers should be used to help construct HTML that requires anything more that the normal control logic, the <?php if ($foo) : and <? foreach($foo....
You will want to avoid cases where you view logic becomes too complex or repetitive, as this results in hard to maintain code.
The example you posted, a <span> tag is returned based on the the number of days ($numberOfDays) arguments passed to it. This is something that is most likely repetitive to check every time you wish to output the 'new' item, hence the need for a view helper.
In your case, what would be ideal, would be to fetch the form via the FormElementManager from within the controller.
class FooController {
function someAction() {
$serviceManager = $this-getServiceLocator();
$formElementManager = $serviceManager->get('FormElementManager');
$myForm = $formElementManager->get('My\Form\Class\Name\Or\Alias');
return new \Zend\View\Model\Model(array(
'form' => $myForm
);
}
}
Calling the form from the FormElementManager will ensure its dependencies are correctly injected and init() called.
In the view you would then render the form using Zend's built in ViewHelpers (Like FormRow taking our complex form object and return all the required HTML)
With the above in place the need for you to 'assign view scripts to the form' disappears as the form can be injected, via the controller, into any view script.
Related
For example I have edit profile page which have a form for editing the summary. the file's name is Index.tpl.
In form I have a text field, I have added the saveSummary() in the controller i.e. controller.php. How can I invoke the given function on clicking on submit button of form.
Answering directly to your question, you should submit the form to /save-summary/ action if it's called saveSummary() in your controller. Of course don't forget to include prefix of the route.
Generally your approach is not correct because you're trying to use different actions for displaying content and processing the form – you can easily do both operations in one action. Check for isPost() and getPost() in other controllers – this methods are used to divide parts of action responsible for simply getting and displaying content and for processing form data.
You can access controller functions by using action handle at the end of function name ex -
class Mymodule_MytestController extends Core_Controller_Action_Standard{
public function saveSummaryAction(){
.......
}
}
I'm writing a website using Slim Framework and Twig.
The sidebar on the website will have dynamically created links and i want to get the links from the database/cache every time a page is rendered, i can do this in each controller action but i would like to do this in my base template so i don't have to get and render the data manually in every controller action.
I have looked through the twig documentation and I haven't seen anything that could be of use besides the include function/tag but i don't see how i could get the data easily.
Is my only option to write an twig extension to do this or is there an easier way?
First of all: templates usually shouldn't contain any type of bussines logic but only render the data you provide it.
However you could write a custom twig function and use that to aquire the menu data from the DB in order to render it in your base template.
Alternativeley you could write some Slim middleware that aquires the data which might be able to inject the data into the template.
Hope that helps.
After reading Anticoms answer i was able to do it by writing a Twig extension.
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('render', array($this, 'render'))
);
}
public function render($template, $controller, $action) {
$app = Slim::getInstance();
return $app->$controller->$action($template);
}
Now i can simply write
{{ render('Shared/sidebar.twig', 'Controller', 'Index') }}
Into my twig template to render the template with the data i want.
Note that my render() function uses slim dependency injection to instanciate the controller at runtime.
In the past I've used a proprietary framework that kinda followed an Objective-C like View Controller scheme.
Basically I was able to in one controller instantiate another and pass it some values like an array of products and then inject the reference to the controller to my view and render it whenever I wanted by issuing: $controllerReference->render();
This could be useful in many cases, eg. if I had a controller responsible for rendering a catalog, I would just pass it an array with all the items I would like to see and it would take take of pagination and displaying the items by itself...
Example:
At \UI\Homepage\Controller.php (the controller responsible for the homepage):
// Instantiate a ProductDisplay Controller
$controllRef = new \UI\ProductDisplay\Controller();
$controllRef->setProducts( ... ); // Inject the product array
// Load the current view for this controller and pass it a reference for the ProductDisplay controller
$this->loadSelfView(["controllRef" => $controllRef]);
At \UI\Homepage\View.php (the view loaded before):
// some html of the view
$controllRef->render(); // Render the ProductDisplay view here!
// some other html
How should this functionality be accomplished in Laravel? From what I've been reading Laravel tries to avoid this kind of actions, why? What are the workarounds?
Thank you.
Here is how I will do this, it only work if the called controller method return a View object like return view('home');):
// Get the controller class from the IOC container
$myController= \App::make(MyController::class);
// Execute the controller method (which return a View object)
$view = $myController->myControllerMethod($someParams);
// Return the View html content
$html = $view->render();
you can use the Controller.php class which is extended by all other controller to make a generic method in it to:
Get a controller instance
Call the right method with x parameters
Return the rendered content if it's a view class OR throw an exception (for exemple)
In recent versions of Laravel, it's possible to use Blade's #inject directive. It seems its main purpose is to inject services but I successfully injected controller actions.
Here is a snippet on what you have to do:
#inject('productController', 'App\Http\Controllers\ProductController')
{!! $productController->index() !!}
Just remember: since you're calling the controller method directly, and not through the router, you'll have to pass all required params. If that action requires a request instance, you may call it this way:
{!! $productController->index(request()) !!}
Probably the view returned by the called action contains HTML code. It's important to wrap the method call within {!! and !!}. Using regular {{ }} tags will cause the HTML code be escaped by the template engine.
I'm developing a web application with Zend Framework 1.12, which is something new to me, and I'm not sure about the way to do something I want to.
EDIT: When I talk about Module, I mean Controller, sorry for that, I still mistake the terms ...
On my home page, the module Index, I made what I wanted to do with it, created several actions and all the stuff, but I'd like to add a search engine I'll make myself.
The problem is that I'd like to create the search engine as a separate module named Search, for example, but put the SearchForm in the home page. Hitting submit would send the datas from the form to the Search module.
I don't quite understand how to do that without having to go to /search to access my form and every associated actions.
Do I have to use a View Helper ?
Also, the searchForm in the front page would be some sort of QuicKSearch and accessing /search would show a more elaborated form for the research.
Can someone explain me how to access the searchForm from the Index module or redirect me to the part of the documentation talking about that ? My research are unsuccessful and Google doesn't help me either.
EDIT: When I talk about Module, I mean Controller, sorry for that, I still mistake the terms ...
First of all, build the searchform as viewHelper, then you can reuse it in several views.
The action attribute in form snippet set to searchModule/controller/action.
Additionaly make research about viewHelpers and Forms in Zend Documentation.
I actually prefer to do this as a an action helper and then just use a standard placeholder view helper to present the search form.
let me demonstrate:
the actual action helper just initiates a form and prepares it for display. I'll leave the form structure to you.
//the action helper
//Just fill in the args for the form to be displayed
class NameSpace_Controller_Action_Helper_Search extends Zend_Controller_Action_Helper_Abstract
{
public function direct($action, $label = null, $placeHolder = null)
{
$form = new Application_Form_Search();
//set the action
$form->setAction($action);
//set the submit button text
$form->search->setLabel($label);
//set the hint text displayed in the form window
$form->query->setAttribs(array('placeholder' => $placeHolder,
'size' => 27,
));
return $form;
}
}
I put the helper in the predispatch method of the controller so that each action in the controller can use the search form with having to build it in every page.
//to use the helper in your controller
class IndexController extends Zend_Controller_Action
{
public function preDispatch()
{
//setup action helper and assign it to a placeholder
$this->_helper->layout()->search = $this->_helper->search(
'/index/display', 'Search Collection!', 'Title');
}
//in your view script
<?php echo $this->layout()->search ?>
I like to put the placeholder in my master layout.phtml so that any time I populate the placeholder it will display. Now all you have to do is style it however you want.
Remember: As with any html form the action parameter is just a url so any valid url can be assigned to the form action. In this example I used the /controller/action parameters, but there are many other ways to pass a url to the form. The url helper comes to mind as good way to do it.
url($urlOptions, $name, $reset, $encode): Creates a URL string based
on a named route. $urlOptions should be an associative array of
key/value pairs used by the particular route.
I think I can't see the tree in the wood.
I'm using Zend Framework, with an layout.phtml which is rendering and partial
<?php echo $this->partial('_header.phtml') ?>
My goal is to render an form from my IndexController into the "_header.phtml" with
<?php echo $this->form; ?>
How can I pass the form to the partial view?
View partials are rendered with a clean variable scope... That is, they do not inherit view variables from the calling Zend_View instance.
There's a few options available to you here:
One, simply call:
echo $this->render('_header.phtml');
instead of using a partial. This file will have access to all your view variables, so you can just assign the form to your view in your controller, like anything else.
Another way is to explicitly pass your form as a variable to the partial, like so:
echo $this->partial('_header.phtml', array('form' => $this->form));
// $this->form inside your partial will be your form
Your other option is to either use placeholders, or layout response segments. Here's an example of placeholders:
In your _header.phtml, or layout... where ever you want the form to render:
<?php echo $this->placeholder('header'); ?>
And in your controller:
$this->view->placeholder('header')->append($form);
// I'm not sure, but you _may_ want to pass in $form->render() here.
// I can't remember if implode() (which is used in placeholders internally)
// will trigger the __toString() method of an object.
This has the added bonus of not polluting your view instance with one-off variables, like the form.
Note: I'll link to the manual pages as soon as the ZF site is back up; 1.9 launch is today, so the site's getting updated currently.
Here's some relevant manual pages:
Placeholder view helper
Partial view helper