How to make variable that can be used in many views? - php

I have a project, it has a structure like this:
http://img526.imageshack.us/img526/2333/92348689.png
I want to make a variable like the following
$templatePath = $this->baseUrl('/application/templates/')`
and it can be used in many views, in many modules. I think I can do it by declaring the variable in Bootstrap.php (application) but I don't know how to do that.

Usually, I just place myself such variables into application bootstrap file. Here's an example:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
protected function _initViewVariables() {
$this->bootstrap('View');
$this->view->fooValue = 'foo';
$this->view->barValue = 'bar';
$config = Zend_Registry::get('config');
if( $config->recaptcha->enabled ) {
$this->view->captcha = true;
$this->view->recaptchaPublicKey = $config->recaptcha->publicKey;
}
else {
$this->view->captcha = false;
}
}
}
I hope it helps!

Base Url is available after routing has been completed (routeShutdown hook) so accessing it in Bootstrap will not work .
So create a controller plugin inside preDispatch() do
public function preDispatch($req) {
$view = new Zend_View();
$view->placeholder('burl')->set(Zend_Controller_Front::getInstance()->getBaseUrl());
}
To access it inside view do like index.phtml
echo $this->placeholder('burl');

You could use the Zend_Registry.
In your bootstrap or wherever in your site just State
Zend_Registry::set("TagLine", "Have a Nice Day");
To use in a view just
<?= Zend_Registry::get("TagLine"); ?>
for extra credit you could also make a view helper for this (there is one with ZF2)
class My_View_Helper_Registry extends Zend_View_Helper_Abstract
{
public function registry($key)
{
return Zend_Registry::get($key);
}
}
In your bootstrap you will add a method like:
protected function _initSiteRegistry(){
Zend_Registry::set("Site_Name", "My Cool Site";
Zend_Registry::set("Site_TagLine", "Have a nice day";
}
Also if you are using the view helper approach you will also want to register the helper path.. you can do this in _initView in the bootstrap.
protected function _initView(){
$view = new Zend_View();
$view->doctype("HTML5");
$view->setEncoding("UTF-8");
$view->addScriptPath(APPLICATION_PATH."/local/views");
// set this as the viewRenderer's view.
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$viewRenderer->setView($view);
$view->addHelperPath("My/View/Helper/", "My_View_Helper_");
return $view;
}

Related

How to correctly pass data from controller to view?

My current implementation:
class SomeController extends AppController
{
function someaction()
{
$d['text'] = "ahoy!";
$this->render("someactionView", $d);
}
}
And in AppController:
function render($file, $data = "")
{
require "views/" . $file . ".php";
}
And the $data will be available in the views file. Is this a correct implementation? Are there any fallacies with this implementation?
And the $data will be available in the views file. Is this a correct
implementation? Are there any fallacies with this implementation?
Basically you do implement it like the most frameworks do. There's a couple of problems with that:
A controller takes an input and sends an output (which breaks the Single-Responsibility Principle)
A view is tightly coupled to HTML. Because of this, you cannot re-use the same view for another stuff, like XML, JSON.
If you do require "views/" . $file . ".php"; in render() method - you again tighly couple it. What if you change the location of views? Then you would have to slightly rewrite your method. This approach merely kills reuse-ability.
To refresh your basic knowledge:
Controller (also known as Editor)
Serves only singular purpose. It changes model state - that is, it should take an input that comes from $_POST, $_GET, $_FILES, $_COOKIE. In controller only variable assignment should be done and nothing more.
class Controller
{
public function indexAction()
{
$this->view->setVar('age', $this->request->getPostParam('age'));
$this->view->setVar('user', $this->request->getPostParam('user'));
//...
}
}
View
A view has a direct access to a model. In order to make make views more re-usable and maintainable you'd better pass required things as function parameters (or via setters)
class View
{
public function render($templateFile, array $vars = array())
{
ob_start();
extract($vars);
require($templateFile);
return ob_get_clean();
}
}
How the view should be initialized and how the variables should be passed to it?
First of all - a view should be instantiated outside MVC-triad. Since a controller writes either to view or model - you'd pass variables to view via controller.
$model = new Model();
$view = new View($model);
$controller = new Controller($view);
// This will assign variables to view
$controller->indexAction();
echo $view->render();
Note : In real world scenario, a model isn't a class, but abstraction layer. I call it Model for demonstration purposes.
IMO the render() method belongs to the view and not to the controller. The code should look like this:
Controller:
class SomeController extends AppController
{
function someaction()
{
$d['text'] = "ahoy!";
$view = new SomeActionView();
$view->assign('data', $d);
echo $view->render();
}
}
View Base Class:
class View
{
protected $data;
function render($template) {
ob_start();
// you can access $this->data in template
require "views/" . $template . ".php";
$str = ob_get_contents();
ob_end_clean();
return $str;
}
function assign($key, $val) {
$this->data[$key] = $val;
}
}
Extend View class
class SomeActionView extends View
{
public function render($template = 'someActionTemplate') {
return parent::render($template);
}
}
Is this a correct implementation? Are there any fallacies with this implementation?
Short answer: no and several.
First of all, what you have there is no a view. It's just a dumb php template. Views in MVC are instance, that contain the UI logic for the application. They pull information from model layer and, based on information they receive, create a response. This response can be simple text, JSON document, a HTML page assembled from multiple templates or simply a HTTP header.
As for controller, it's only task is to alter the state of model layer and (on rare occasions) the current view. Controllers do not initialize the views nor do the populate templates.

rendering different view engine in zend depending on extension

I have a normal phtml template and another one in HAML. So somewhere in my Bootstrap.php:
protected function _initView()
{
$view = new Haml_View();
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
$viewRenderer->setView($view);
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
return $view
}
I initialize to use my Haml_View, but what i want is if the script filename has an extension .haml, it'll use Haml_view if not, then it'll use the regular Zend_view.
So i guess my question is, is there any way to find out what the current view script filename will be use?
Thanks
The basic workflow of a ZF MVC request is as follows:
Application bootstrapping
Routing
Dispatch
Zend_Application takes care of only the first item in that list,
bootstrapping. At that time, we have no idea what the request actually
is -- that happens during routing. It's only after we have routed that
we know what module, controller, and action were requested.
Source: http://weierophinney.net/matthew/archives/234-Module-Bootstraps-in-Zend-Framework-Dos-and-Donts.html
So you can't switch the view class based on script suffix in the bootstrap because the routing has not occured yet. You could do it in a FrontController plugin as early as routeShutdown, but I feel it's more natural to do it in an Action Helper. The normal methods to figure out the view script path are in Zend_View and Zend_Controller_Action_Helper_ViewRenderer. Both of these are easily available in an Action Helper.
Zend_Controller_Action_Helper_ViewRenderer is also an Action Helper and it needs to init before, so let's do our switch after the init, in the preDisptatch call of an Action Helper.
First, you need to register your helper. A good place is in the bootstrap with your view:
protected function _initView()
{
$view = new Haml_View();
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
$viewRenderer->setView($view);
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
return $view;
}
protected function _initHelpers()
{
Zend_Controller_Action_HelperBroker::addHelper(
new Haml_Controller_Action_Helper_ViewFallback()
);
}
And the helper would look like this:
class Haml_Controller_Action_Helper_ViewFallback extends Zend_Controller_Action_Helper_Abstract
{
public function preDispatch()
{
/** #var $viewRenderer Zend_Controller_Action_Helper_ViewRenderer */
$viewRenderer = $this->getActionController()->getHelper('ViewRenderer');
/** #var $view Haml_View */
$view = $viewRenderer->view;
/**
* what i want is if the script filename has an extension .haml,
* it'll use Haml_view if not, then it'll use the regular Zend_view
*/
$viewRenderer->setViewSuffix('haml');
$script = $viewRenderer->getViewScript();
if (!$view->getScriptPath($script)) {
$viewRenderer->setView(new Zend_View());
$viewRenderer->setViewSuffix('phtml');
$viewRenderer->init();
}
}
}
If there is no file with the haml extension in the default path, we assume there is one with phtml extension, and we modifiy the ViewRenderer accordingly. Don't forget to init the ViewRenderer again.
Something along the lines of:-
if(!file_exists('path/to/view.haml'){
$view = new Zend_View();
$viewRenderer->setView($view);
}
May work, although I haven't tested it.
Edit:
You could try this in your controller:-
class IndexController extends Zend_Controller_Action {
public function init()
{
$paths = $this->view->getScriptPaths();
$path = $paths[0];
$script = $path . $this->getHelper('viewRenderer')->getViewScript();
if(!file_exists($script)){
$this->viewSuffix = 'hmal';
}
}
public function indexAction()
{
}
}
I'm not 100% happy with the $path = $paths[0] bit, but I don't have time to look into it any further. Hopefully that will point you in the right direction.

Going from the Controller to the View

I am working on creating my own very simple MVC and I am brainstorming ways to go from the controller to the view. Which involves sending variables from a class to just a plain old PHP page.
I am sure that this has been covered before, but I wanted to see what kind of ideas people could come up with.
//this file would be /controller/my_controller.php
class My_Controller{
function someFunction(){
$var = 'Hello World';
//how do we get var to our view file in the document root?
//cool_view.php
}
}
Some kind of hashtable is a good way to do that. Return your variables as association array which will fill all the gaps in your view.
Store your variables as a property in your controller object, then extract them when rendering
class My_Controller {
protected $locals = array();
function index() {
$this->locals['var'] = 'Hello World';
}
protected function render() {
ob_start();
extract($this->locals);
include 'YOUR_VIEW_FILE.php';
return ob_get_clean();
}
}
You can define those magic __get and __set methods to make it prettier
$this->var = 'test';
I'm also developing my own simple MVC and the most simple way to do it is ...
class My_Controller
{
function someFunction() {
$view_vars['name'] = 'John';
$view = new View('template_filename.php', $view_vars);
}
}
View class
class View
{
public function __construct($template, $vars) {
include($template);
}
}
template_filename.php
Hello, <?php echo $vars['name'];?>
I highly recommend you to take a look at PHP Savant http://phpsavant.com/docs/
I'd checkout Zend_View and how it accomplished view rendering.
You can get the source of View and AbstractView on github - unfortunaly I don't find the current repository (in svn) that easy to browse.
Essentially the view variables are contained in a View object (which your controller would have access to), then the template (plain old php document) is rendered inside that object. That method allows the template access to $this.
It would be something like:
<?php
class View
{
public function render()
{
ob_start();
include($this->_viewTemplate); //the included file can now access $this
return ob_get_clean();
}
}
?>
So in your controller:
<?php
class Controller
{
public function someAction()
{
$this->view->something = 'somevalue';
}
}
?>
And your template:
<p><?php echo $this->something;?></p>
In my opinion this pattern allows you much flexibility with the view.
I created my own MVC for the free PHP course I'm conducting for a handful of people wanting to get better at PHP.
By far the best way to do this is to use the Command + Factory pattern.
E.g.
interface ControllerCommand
{
public function execute($action);
}
In each controller:
class UserController implements ControllerCommand
{
public function execute($action)
{
if ($action == 'login')
{
$data['view_file'] = 'views/home.tpl.php';
}
else if ($action == 'edit_profile')
{
$data['view_file'] = 'views/profile.tpl.php';
$data['registration_status'] = $this->editProfile();
}
return $data;
}
}
From your main front controller:
$data = ControllerCommandFactory::execute($action);
if (!is_null($data)) { extract($data); }
/* We know the view_file is safe, since we explicitly set it above. */
require $view_file;
The point is that every Controllercommand class has an execute function and that returns its view and any data for that view.
For the complete MVC, you can access the open source app by emailing me at theodore[at]phpexperts.pro.

How to move the "views" of Zend_Layout

Normally it would be in such a structure:
../application/modules/somemodule/views/scripts/index/index.phtml
How I move it to:
../application/templates/somemodule/template1/views/......
../application/templates/somemodule/templateTWOOMG/.......
??
You need to play with: $viewRenderer::setViewBasePathSpec();
For example in frontController plugin, (or easier, but not so flexible, in Bootstrap.php):
$templateName = 'myTemplate';
$bootstrap = $this->getBootstrap();
$bootstrap->bootstrap('layout');
if ($bootstrap->hasResource('layout')) {
$layout = $bootstrap->getResource('layout');
$layout->setLayoutPath($basePath . '/layouts/scripts/');
}
$bootstrap->bootstrap('view');
if ($bootstrap->hasResource('view')) {
$view = $bootstrap->getResource('view');
} else {
$view = new Zend_View;
}
$vr = Zend_Controller_Action_HelperBroker::getExistingHelper("viewRenderer");
$vr->setViewBasePathSpec($basePath."/modules/:module/$templateName/views/");
Take a look on getters and setters in frontController, view, layout and viewRenderer classes. There are plenty of methods which allow to customize default directory structure.
I did it with a plugin, and set a variable in my config to specify the name of the theme.
class Application_Plugin_ThemeSetup extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
// load up the config and the view object
$objConfig = Zend_Registry::get('config');
$objView = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');
// set path for views based on theme designation in config
$theme = ! empty($objConfig->theme->name) ? $objConfig->theme->name : 'default';
$Renderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$Renderer->setViewBasePathSpec(APPLICATION_PATH."/views/$theme");
// add some variable to the view at high level
$objView->themeName = $objConfig->theme->name;
$objView->themeDescription = $objConfig->theme->description;
}
}

How to test controllers with CodeIgniter?

I have a PHP web application built with CodeIgniter MVC framework. I wish to test various controller classes. I'm using Toast for unit testing. My controllers have no state, everything they process is either saved into session or passed to view to display. Creating a mock session object and testing whether that works properly is straightforward (just create a mock object and inject it with $controller->session = $mock).
What I don't know, is how to work with views. In CodeIgniter, views are loaded as:
$this->load->view($view_name, $vars, $return);
Since I don't want to alter CI code, I though I could create a mock Loader and replace the original. And here lies the problem, I cannot find a way to derive a new class from CI_Loader.
If I don't include the system/libraries/Loader.php file, the class CI_Loader is undefined and I cannot inherit from it:
class Loader_mock extends CI_Loader
If I do include the file (using require_once), I get the error:
Cannot redeclare class CI_Loader
Looks like CI code itself does not use require_once from whatever reason.
Does anyone here have experience with unit testing CodeIgniter powered applications?
Edit: I tried to inject a real loader object at run-time into a mock class, and redirect all calls and variables with __call, __set, __get, __isset and __unset. But, it does not seem to work (I don't get any errors though, just no output, i.e. blank page from Toast). Here's the code:
class Loader_mock
{
public $real_loader;
public $varijable = array();
public function Loader_mock($real)
{
$this->real_loader = $real;
}
public function __call($name, $arguments)
{
return $this->real_loader->$name($arguments);
}
public function __set($name, $value)
{
return $this->real_loader->$name = $value;
}
public function __isset($name)
{
return isset($this->real_loader->$name);
}
public function __unset($name)
{
unset($this->loader->$name);
}
public function __get($name)
{
return $this->real_loader->$name;
}
public function view($view, $vars = array(), $return = FALSE)
{
$varijable = $vars;
}
}
Alternatively, you could do this:
$CI =& get_instance();
$CI = load_class('Loader');
class MockLoader extends CI_Loader
{
function __construct()
{
parent::__construct();
}
}
Then in your controller do $this->load = new MockLoader().
My current solution is to alter the CodeIgniter code to use require_once instead of require. Here's the patch I'm going to send to CI developers in case someone needs to do the same until they accept it:
diff --git a/system/codeigniter/Common.php b/system/codeigniter/Common.php
--- a/system/codeigniter/Common.php
+++ b/system/codeigniter/Common.php
## -100,20 +100,20 ## function &load_class($class, $instantiate = TRUE)
// folder we'll load the native class from the system/libraries folder.
if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT))
{
- require(BASEPATH.'libraries/'.$class.EXT);
- require(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT);
+ require_once(BASEPATH.'libraries/'.$class.EXT);
+ require_once(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT);
$is_subclass = TRUE;
}
else
{
if (file_exists(APPPATH.'libraries/'.$class.EXT))
{
- require(APPPATH.'libraries/'.$class.EXT);
+ require_once(APPPATH.'libraries/'.$class.EXT);
$is_subclass = FALSE;
}
else
{
- require(BASEPATH.'libraries/'.$class.EXT);
+ require_once(BASEPATH.'libraries/'.$class.EXT);
$is_subclass = FALSE;
}
}
I can't help you much with the testing, but I can help you extend the CI library.
You can create your own MY_Loader class inside /application/libraries/MY_Loader.php.
<?php
class MY_Loader extends CI_Loader {
function view($view, $vars = array(), $return = FALSE) {
echo 'My custom code goes here';
}
}
CodeIgniter will see this automatically. Just put in the functions you want to replace in the original library. Everything else will use the original.
For more info check out the CI manual page for creating core system classes.
I'm impressed by the code you are trying to use.
So now I'm wondering how the 'Hooks' class of CodeIgniter could be of any help to your problem?
http://codeigniter.com/user_guide/general/hooks.html
Kind regards,
Rein Groot
The controller should not contain domain logic, so unit tests make no sense here.
Instead I would test the controllers and views with acceptance tests.

Categories