I am new to zend framework. I found code in bootstrap file as below.
protected function _initDoctype()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->doctype('XHTML1_STRICT');
}
can anyone explain me what "$this->bootstrap('view');" this mean?
From the same page where your snippet comes:
Now that we have a view, let's flesh
out our _initDoctype() method. In it,
we will first ensure the View resource
has run, fetch the view object, and
then configure it.
It sets up the view resource so that you can access it. Without it the following line would not return anything to put in the $view variable and you'd get the error:
Fatal error: Call to a member function doctype() on a non-object.
It's pretty straightforward, it literally bootstraps the View object.
Bootstraping is a step where you set up (configure and instantiate) your object, resolve dependencies, etc.
It is done because the View Object must be set before to be able to set a Doctype.
Related
I am trying to implement a simple roll-a-dice service and use it in a .phtml file. I know my issue has been reported often on SO, but I could not find a solution in other questions.
I get the following error message:
Fatal error: Call to a member function getDiceResult()
on a non-object in rolladice.phtml on line 11
Here is line 11:
<?php
$result = $this->rollADiceService->getDiceResult();
echo "<p>Roll-a-dice result: ".$result."</p>";
?>
The controller is set-up as following:
class RollADiceController extends AbstractActionController
{
private $rollADiceService;
public function setPluginManager(PluginManager $plugins) {
parent::setPluginManager($plugins);
$this->rollADiceService = $this->getServiceLocator()
->get('RollADiceService');
}
...
In Module.php, I have:
public function getServiceConfig()
{
return array(
'factories'=>array(
'LoginLogoutService' => function() {
return new LoginLogoutService();
},
'RollADiceService' => function() {
return new RollADiceService();
},
),
);
}
I am using the same technique (i.e., setPluginManager) to retrieve instances of my services in other controllers without issues. What am I doing wrong?
P.S.: Using a debugger, I can see that setPluginManager() is called, but that the $this->rollADiceService variable is initialized with null. Why?
Ultimately, there were two issues:
i) I refactored my code to pass the result of the call to rollADiceService->getDiceResult() as a variable which $this can access.
ii) Hijacking setPluginManager() is not a recommended practice. I've implemented factories for my controllers depending on services.
your problem is realy simple I think.
<?php
$result = $this->rollADiceService()->getDiceResult();
echo "<p>Roll-a-dice result: ".$result."</p>";
?>
Add these brackets after rollADiceService should fix your problem. Without brackets, Zend View instance try to access to view variable named rollADiceService, which is obviously NULL.
By adding brackets, you tell to View it should look for rollADiceService in registered view helpers.
I hope it helps :)
I'm trying to create my own little PHP-Framework just for fun and to learn.
But now I stuck with the View.
class Index extends Controller {
function __construct() {
parent::__construct();
$this->view->msg = 'This message is sended over the view.';
$this->view->content = 'This is the INDEX-Content.';
$this->view->render('index/index');
}
public function something() {
// do something
// and render it
$this->view->content = 'This is the content from something.'
}
So what I can do is to misuse the __destruct and render here my output. But I guess that is against the purpose of this method.
When I compare my intention with e.g. Zend Framework or Laravel they use e.g. an after() method to render a view.
But I do not understand which method can do this. The constructor is the first, the destructor the last and everything between it has to be called to work.
Are there any "magic" methods for this?
You should handle your HTTP I/O
This is how you can output
This is how a request is executed
This is how the action is triggerd
Sniff through the repo as much as you can, Kohana is a simple yet powerfull framework. (you can learn a thing or two)
You can do something like this in your main Controller class :
public function __call($method,$arguments) {
if(method_exists($this, $method)) {
call_user_func_array(array($this,$method),$arguments); //this is where the function is called
$this->render();
}
}
You can eliminate in hear constructors, destruct and other functions that you do not want to automatically render.
You can also have a variable in your main Controller class, autoRender set default to false and just set it to true when you want to produce a predefined output.
Also in the _call function, you can use the $method variable to have a predefined name for your view. Like for example lets say you would have a folder in your framework called Views and in there you would have a file called something.view_extension.
You can send to render like this : $this->render($method.'.view_extension');
Just a bulk idea you can work around. :)
I have the following code but it does not seem to want to work chained.
$this->view->setData($class_vars);
$this->view->render('addview');
The above works and runs fine but when i try to do the following:
$this->view->setData($class_vars)->render('addview');
I get the following error:
Fatal error: Call to a member function render() on a non-object in....
But the strange thing is when i call it the other way:
$this->view->render('addview')->setData($class_vars);
It runs, but I need the setData to run first as this sets up the var for the actual view, so even though i get the view its got errors where the vars should be? Both methods are public?
Thanks You
Does setData() return the view object (i.e. it has return $this; line)? If not... well it should if you want it to work this way.
For further reference. This technique is called 'fluent interface' and is described here:
http://www.martinfowler.com/bliki/FluentInterface.html
I was going through some tutorial and documentation about zend framework, most of things made sense until i came across $this variable in /application/layout/scripts/layout.phtml, it was mentioned that $this is an instance of the view object that was created during bootstrapping.
to my knowledge you cannot use $this as the variable name as because $this is a reserved keyword for php used to refer the same object within the class context. any attempt to use it as a variable will result in Fatal error with the following error message Fatal error: Cannot re-assign $this and as per the author's statement There is a variable, $this, available which is an instance of the view object, i am unable to understand the theory behind this. how come $this is being used out of the class context?
It's actually being used in the context of an object. You should look at the code yourself, but the basic idea behind render() (which is the toString method by proxy):
public function render()
{
//Start output buffering
ob_start();
include $this->viewScript;
//Get the content from the include
$content = ob_get_flush();
return $content;
}
Zend Framework does it a bit more complexly so that it's a bit more flexible than that, but it's the basic idea.
Then, inside of the viewScript, it's technically inside of the render() method just as if the code were literally in that "include ..." place. (Oversimplifying that, but the general idea holds.)
It's probably being used in the class context. Imagine the view being created along the following lines:
class View {
public function render($viewfile = 'views/myviewfile.phtml') {
ob_start();
include($viewfile);
$view_data = ob_get_contents();
ob_end_clean();
echo $view_data;
}
}
The view presentation process is probably more complex than simply capturing an included view file, but you can see how $this would be available to the view when View::render() is called.
I am trying to understand how classes and functions work more.
So i have written a class with 2 functions inside it. Then initiated the class with
$fifaadmin = new FifaAdmin;
When I try to call this class from another page i get the following error
Call to a member function leagueToReplace() on a non-object
What am i doing wrong? Is there an obvious answer?
Thanks
You need to instantiate the class on each page. Each script is executed independently.
Sounds like $fifaadmin didn't instantiate the object correctly.
What does this say?
var_dump($fifaadmin instanceof FifaAdmin);
It should return true if it is set up correctly. Try it just before you call a method on it.
When you say another page, do you mean from a PHP include or a new URL (and therefore request)?
You will need to instantiate it on every request, as HTTP is stateless.
Depending on what your code does, you might want to use static methods:
// common.php
class Common {
public static function calculate($x, $y) {
return $x + $y;
}
}
// another PHP file: (you still need to include common.php
// you won't need to instantiate the class
echo Common::calculate(10, 20);
If you want to use an instance of class in different pages, you need to serialize it and save it somewhere (session is ok) and de-serialize it when you want use it.
More detail about serialize in PHP here http://php.net/manual/en/function.serialize.php