I would like to include multiple views within a view in ZF2.
I read this link:
http://framework.zend.com/manual/current/en/modules/zend.view.quick-start.html
but there is a problem. In this way, I have to pass the values that are within a view, like this:
$secondView = new ViewModel (array('var1' => $var1 ......));
In this mode the controller and second action are bypassed.
Is there a way to include a view without bypassing them? I would like variables to be passed from the second action controller, like an include php statement
If I understood correctly your question, I think that what you're asking is not possible.
My suggestion would be to move the retrieval of the data you need for the secondView somewhere else, away from the controller, and call it both from the second controller and form the first controller to pass them to the secondView.
If you really want to go on with your approach, the only possibility I see is to use javascript and ajax calls to retrieve the partials you need in your view
You can use partials for that.
In your module.config.php under the 'view_manager' key you define a template map for your partial:
'view_manager' => array(
'template_map' => array(
'myPartial' => __DIR__ . '/../view/mymodule/partial/myPartial.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
Then in the view container you can use that partial using the partial() View Helper:
<div><?php echo $this->partial('myPartial', array('var1' => 'value1'); ?></div>
You can also pass variables to your partial. Those variables are referenced in your partial like any other view variable:
echo $var1;
Related
well i trying build a website on ZendFrameWork 2 i want execute various actions in my layout main like a fild of search, or a form for loguin etc. each one of this i believe should are in views diferents with your actions, how i do for references this actions with your views that will return this element for i put where i want in my layout. like the helper $this->content; where it return the context view and action no just the view with no function, which are the best way for i do this or a way alternative
I think what you need are view partials. You can read on view partials in the ZF2 official documentation.
You have to create your partials and then you register them under your template_map inside the view_manager config.
'view_manager' => array(
//...
'template_map' => array(
'my/form' => __DIR__ . '/../view/partials/form.phtml',
'my/partial' => __DIR__ . '/../view/partials/partial.phtml',
)
),
Then in every layout you can simply embed them like this:
echo $this->partial('my/form');
echo $this->partial('my/partial');
If you need to pass variables to your partials you can pass a second argument:
echo $this->partial('my/form', array('foo' => 'bar'));
There are different modules and all of them returns ViewModel in the actions. But somehow, ViewModel acting weird a bit in one of the modules.
I am saying;
$view = new ViewModel(array('data' => $someContent));
$view->setTemplate('a valid path to template');
return $view;
and getting an empty page.
If I put an exit() statement at the end of related template like
<!DOCTYPE html>
<html>
...
</html>
<?php exit(); ?>
I can get the expected output because script ends there but I lost the output otherwise.
If I say *var_dump($view)*, I can see that the $view is an instance of Zend\View\Model\ViewModel.
There is no error, just an empty output and even the notice warnings are visible. So, it doesn't throw any exception, error, warning, notice etc.
To remind that again, it just happens in a specific module but that module are not different the others actually.
I am not a ZF guru and I am working on someone else's codes, so please give me a start point to able to find that problem.
Thanks in advance.
edit : I have an extra info;
It works if I use JsonModel instead of ViewModel and as you may know, JsonModel extends the ViewModel.
Since you have not posted your controller action properly , this is the guess what I could do on your problem .
In Zend framework 2 there are various types of controllers from which you will be extending your controllers with in your modules .
for example in case if you extend your controller from AbstractActionController your view will be returned properly .
So the problem here is your other modules have controllers extending AbstractActionController . This module which is not returning your view properly might not be extending it . Instead it might be extending other controllers such as restfulcontrollers
You should also check in module.php file of your module to check whether you have any strategies eg json strategy applied on bootstrap for this module from module.config.php .
eg.
return array(
'view_manager' => array(
'strategies' => array(
'ViewJsonStrategy',
),
),
)
Also you have check your module.config.php file whether you have proper specification for your viewmanager to your template .
eg .
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
Hope this helps .
I'm creating an app in CakePHP which requires me to run 'multiple' apps within one CakePHP installation. Something like I have n controllers that behave the same for all applications, but they only differ when I call the database - anyway, I need to create a route which behaves something like this:
/app1/controller/action/a/b/c
/app2/controller/action/a/b/c
(where app1 and app2 are alphanumeric strings that can change to anything)
That would be routed to something like:
/controller/action/app1/a/b/c (or the same for app2, and so on)
The routed route could be just /controller/action/a/b/c too, but I need to have a way to access the app1 / app2 parts of the URL within the controller (for further processing within the controller). Is there a way to do this in CakePHP? Thanks.
Slightly related question: When the above is accomplished, is there a way to set a 'default' app-name (like when I attempt to access /controller/action/a/b/c it will automatically be routed to the equivalent of typing /global/controller/action/a/b/c?)
Thanks!
Effectively: What I want is just to use Routing (or any other CakePHP 'method' that can do this) to handle URLs like /foobar/controller/action/the/rest to /controller/action/the/rest and pass "foobar" to the controller, somehow. "Foobar" is any alphanumeric string.
In app/Config/routes.php add:
Router::connect(
'/:app/:controller/:action/*',
array(),
array( 'pass' => array( 'app' ))
);
This will pass the value of app as the first argument to the action in your controller. So in your controller you would do something like:
class FoosController Extends AppController {
public function view_something($app, $a, $b, $c) {
// ...
}
}
When you request /myApp1/foos/view_something/1/2/3 the value of $app would be 'myApp1', the value of $a would be 1, etc.
To connect other routes, before the above, you can add something like:
Router::connect(
'/pages/:action/*',
array( 'app' => 'global', 'controller' => 'pages' ),
array( 'pass' => array( 'app' )) // to make app 1st arg in controller
);
Instead of routing, you should use Model attribute -> dbconfig to change the databases dynamically. Also you should also have to send some arguments to the method by which you can identify which database needs to be connected with your application.
Below i have an ajax tab example:
$this->widget('zii.widgets.jui.CJuiTabs', array(
'tabs' => array(
'StaticTab 1' => 'Content for tab 1',
'StaticTab 2' => array('content' => 'Content for tab 2', 'id' => 'tab2'),
// panel 3 contains the content rendered by a partial view
'AjaxTab' => array('ajax' => $this->createUrl('/AjaxModule/ajax/reqTest01')),
),
// additional javascript options for the tabs plugin
'options' => array(
'collapsible' => true,
),
));
but i don't know what happens in /AjaxModule/ajax/reqTest01.
There is a missing part in this example, the rendering view, and i don't know how to design it so that the ajax call to work. Thanks.
According to the code you have put up, specifically this line:
'AjaxTab' => array('ajax' => $this->createUrl('/AjaxModule/ajax/reqTest01')),
We know that we need ajaxmodule, ajax controller, reqtest01 action, so do the following steps:
First
Create a module, it'll have to be named AjaxModule.
Second
Create a controller in this AjaxModule named Ajax.
Third
Create an action within this Ajax controller, named ReqTest01.
Within this action you can either directly echo html, or use renderPartial(), to render a view file partially for ajax.
So your controller Ajax and the action within looks somewhat like this
<?php
class AjaxController extends Controller
{
public function actionIndex()
{
$this->render('index');
}
public function actionReqTest01(){
// directly echoing output is hardly of any use, like echo "Directly echoing this";
$this->renderPartial('rendpar_ajax'); // renderPartial is way better as we have a view file rendpar_ajax.php that we can manipulate easily
}
}
Fourth
Now we can code the rendpar_ajax.php view file, create this file in the views folder of the ajaxController controller in the AjaxModule module.
<?php
// rendpar_ajax.php file for ajax tab
// have any code here, use widgets, form, html helper etc
echo "<h1>AjaxModule--AjaxController--actionReqTest01</h1>";
echo "<p>This view is partially rendered</p>";
Read more about creating modules, controllers, actions, and how they are used, and how the yii directory hierarchy works.
Good luck!
Edit: Note that to a view we can also pass dataproviders for getting complex dynamic views.
Im using "Zend form". What should I do to get correct "action" parameter in my FORM tag?
For example, the form Form_Search used in SearchController in indexAction. Where and how should I set the action parameter to form to make it "/my/app/directory/search/index"?
Update: "action" parameter should be the same as the form will assume if not set. I just need the param in FORM to simplify JS coding.
You can use setAction method of Zend_Form, e.g.:
$yourForm->setAction('/my/app/directory/search/index');
Hope this is what you are looking for.
EDIT:
You can retrieve many info about the request, in an action, using $this->getRequest() method that returns an instance of Zend_Controller_Request_Http. For example, if to get basePath and everything between the BaseUrl and QueryString you can do:
$infoPath = $this->getRequest()->getPathInfo();
$basePath = $this->getRequest()->getBaseUrl();
$yourForm->setAction($basePath . $infoPath);
Zend_Controller_Request_Http has other methods that might be useful to you.
You can use the url() view helper to create your action urls.
$view->url(array(
'module' => 'mymodule',
'controller' => 'mycontroller',
'action' => 'myaction',
'param1' -> 'myvalue1',
// etc
), 'default');
// 'default' here refers to the default route.
// If you specify a custom route, then that route will be used to
// assemble() the url.
Depending where you call $form->setAction(), you can access to the $view in different ways.
In the controller: $this->view
In the form itself: $this->getView()
In a view script: $this
In view helper extending Zend_View_Helper_Abstract: $this->view
Hope it helps!
Just an addition to previous answers. Kind of "industry standard approach" is to render/validate/process the form all on the same page. Then there is no need to set the action as it's filled with current URL.
Exception is for example serch field in layout. Then use $form->setAction($url). But always remember to echo the form in the search action. If the form value is invalid, ZF would expect you to render it in page with the errors. Or you can use $form->getErrors().
`$this->setAttribs(array(
'accept-charset' => 'utf-8',
'enctype' => Zend_Form::ENCTYPE_URLENCODED,
'method' => 'get',
'action' => '/controller/action/param/attr'
))`