I try to follow this tutorial, but I can't get it to work:
http://weierophinney.net/matthew/archives/246-Using-Action-Helpers-To-Implement-Re-Usable-Widgets.html
I did everything as described, but I don't know how to make it available in my controllers. My filesystem looks like this:
- application
- controllers
- IndexController.php
- modules
- user
- configs
user.ini
- controllers
- forms
Login.php
- helpers
HandleLogin.php
- views
- scripts
login.phmtl
profile.phtml
Bootstrap.php
- views
How do I use the HandleLogin Helper in my IndexController? I really have no idea and I'm looking an trying for more then a day and I almost want to throw my PC out of the window ;). So any help would be appreciated!
Looks like the widget plugin is not called anywhere.
Few things to check:
Do you have a Bootstrap.php file for the module?
Does this bootstrap file has _initWidgets() method?
Does this method call:
$widget = new Module_Widget_Name; // is it callable?
Zend_Controller_Action_HelperBroker::addHelper($widget);
Have you registered widget resource?
public function _initResourceLoader()
{
$loader = $this->getResourceLoader();
$loader->addResourceType('helper', 'helpers', 'Helper');
$loader->addResourceType('widget', 'widgets', 'Widget');
return $loader;
}
Does application.ini contains resources.modules[] = line?
You dont. The point of the tutorial is to create a reusable widget that runs independent from any specific controllers. When the application receives a request, it will run through it's dispatch cycle and trigger the action helper on preDispatch automatically:
Now, let's look at the action helper itself. As a reminder, action helpers can define hooks for init() (invoked by the helper broker each time it is passed to a new controller), preDispatch() (invoked prior to executing the controller's preDispatch() hook and executing the action itself), and postDispatch() (executed after the action and the controller's postDispatch() routine).
The helper will fetch the current controller (whichever that may be for that request) to get the View instance and configure it with the form
Related
I do not understand why the following code does not work. Perhaps I do not understand something with the EventManger/SharedEventManager ind Zend Framework 2.
For now I could not find anything about this on the internet.
Could it be that the instance of the IndexContoller is already destroyed at EVENT_RENDER and not constructed at EVENT_ROUTE? Perhaps this is the case or am I missing something here?
class IndexController extends AbstractActionController
{
public function routeEventOccured() {
echo 'test';
}
public function renderEventOccured() {
echo 'test';
}
public function __construct()
{
$this->getEventManager()->attach(MvcEvent::EVENT_ROUTE, array($this,
'routeEventOccured'));
$this->getEventManager()->attach(MvcEvent::EVENT_RENDER, array($this,
'renderEventOccured'));
}
}
Zend Framework 2 uses the concept of event. One class can trigger an event,
and other classes may listen to events. Technically, triggering an event means just calling another class' "callback" method. The event management is implemented inside of
the Zend\Mvc\EventManager component.
The application's "life" consists of several stages. Each application life stage is initiated by the application by triggering an event. Other classes (either belonging to Zend Framework or specific to your application) may listen
to events and react accordingly.
Below, the four main events (life stages) are presented:
Bootstrap. When this event is triggered by the application, a module has a chance to
register itself as a listener of further application events in its onBootstrap()
callback method.
Route. When this event is triggered, the request's URL is analyzed using a router class (typically, with
Zend\Mvc\Router\Http\TreeRouteStack class. If an exact match between the URL and a route
is found, the request is passed to the site-specific controller class assigned to the route.
Dispatch. The controller class "dispatches" the request using the corresponding action method
and produces the data that can be displayed on the web page.
Render. On this event, the data produced by the controller's action method are passed for rendering to
Zend\View\Renderer\PhpRenderer class. The renderer class uses a
view template file for producing an HTML page.
I've created Yii quiz module which I also want to use in Facebook app (Facebook side).
It can be done trough iframe, but generated page have got also menus and many other unnecessary (in this case) stuff. Is it possible in Yii to show only generated module code without rest of the website?
Try and use a different layout for that module. First you can add a layout folder and layout files to that module, say : /protected/modules/quiz/views/layouts/quizlayout.php. So this new quizlayout.php should be your layout for all the views in this module.
To do that you can set the layout property of the quizmodule in the QuizModule class's init(), like so (in QuizModule.php):
class QuizModule extends CWebModule {
public function init() {
// this method is called when the module is being created
// you may place code here to customize the module or the application
// import the module-level models and components
$this->setImport(array(
'quiz.models.*',
'quiz.components.*',
));
$this->layout='quizlayout';
}
//...
}
Now by default, gii generated modules' controllers are subclasses of the Controller class in component/Controller.php file. And that Controller class defines a layout, so if you have that same structure, then the above method will not work, and you'll have to override the layout within your modules' controllers. However instead of going inside each controller and adding a line, you can instead do this in the beforeControllerAction($controller, $action) function in QuizModule.php :
public function beforeControllerAction($controller, $action) {
if(parent::beforeControllerAction($controller, $action)) {
// this method is called before any module controller action is performed
// you may place customized code here
$controller->layout='quizlayout';
return true;
}
else
return false;
}
Edit:
Of course your quizlayout.php should not have code for menus, and any extra stuff, but at the very least the echo $content line should be there, as also mentioned in eskimo's answer.
In you protected/views there is a file called "main.php"
This is your main layout file, that gets rendered around any view called by $this->render
To remove the menu etc.. remove everything within the body except for the line:
<?php echo $content; ?>
Obviously leave in all the stuff in the head (.css files etc...)
I'm starting to work with zend framework 1.12 and I ran into a little problem which I don't seem to be able to fix.
Up untill now i've done everything in the application, but now I want to build a module that handles all stuff that is related to settings.
Therefor i've created a new module and added a controller into it. This module automatically takes the layout from the application, which is what I want.
In this layout I use a view helper which works when I load a controller/action that is in the application folder. But when I try to load the layout around my controller inside my module the view helper is not available.
I hope I'm making sense and I would appreciate your help on this one!
Cheers!
If I understand you correctly you need to setup your view helper path in the bootstrap or application.ini, I do it in bootstrap:
protected function _initView()
{
//Initialize view
$view = new Zend_View();
//add custom view helper path
$view->addHelperPath('/../library/Namespace/View/Helper');
//do more stuff if needed
//add it to the view renderer
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
'ViewRenderer');
$viewRenderer->setView($view);
//Return it, so that it can be stored by the bootstrap
return $view;
}
also make sure your module includes it's own bootstrap file, this makes it possible to load resources to the module:
//at /application/modules/module/bootstrap.php
class Module_Bootstrap extends Zend_Application_Module_Bootstrap
{
//just an empty class is enough
}
hope this helps
I just ask, is it bad idea to extend Zend_View and register like Front Controller plugin after router is ready, because i need, $request to get active module name to show Zend_View where are my templates ?
public function routeShutdown(Zend_Controller_Request_Abstract $request){
$view = new View($config, $request);
$viewHelper = new Zend_Controller_Action_Helper_ViewRenderer($view);
Zend_Controller_Action_HelperBroker::addHelper($viewHelper);
}
And after that i get $request->getModuleName(); and make the my ScriptPaths
I have tried this method
$viewHelper->setViewScriptPathSpec(':controller/:action.:suffix')
But can't work.
It's not necessary to extend View.
You could create controller action plugin which will assign request to View initialized by application resource plugin Zend_Application_Resource_View. But assign only what is necessary (module name) and not full request - for security.
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.