Zend Framework Tutorial
|
|
|
Click to rate: |
|
|
|
1,059 votes / avg. rating 33.90%
|
|
Zend_ControllerUsing the controller is pretty intuitive. In fact, I'm writing this tutorial without the luxury of documentation!
Documentation is now available from the ZF website. I begin with Zend_Controller_Front, a front controller. In order to begin understanding how it works, place the following code in your index.php file:
<?php
include 'Zend.php';
Zend::loadClass('Zend_Controller_Front');
$controller = Zend_Controller_Front::getInstance();
$controller->setControllerDirectory('/path/to/controllers');
$controller->dispatch();
?>
If you prefer object chaining, this can instead be written as:
<?php
include 'Zend.php';
Zend::loadClass('Zend_Controller_Front');
$controller = Zend_Controller_Front::getInstance()
->setControllerDirectory('/path/to/controllers')
->dispatch();
?>
Now, when you make a request for /foo/bar, you get an error. That's good! It lets you know something is happening. The major complaint is that IndexController.php is not found.Before you create this file, it's helpful to understand how the framework expects you to organize things. The framework breaks a request down into parts and, in the case of a request for /foo/bar, foo is the controller, and bar is the action. The default value for each is index. When foo is the controller, the framework looks for a file called FooController.php in the controllers directory. Because this does not exist, the framework falls back to IndexController.php. Not finding either, it reports the error. To continue, create IndexController.php in the controllers directory (which you set with setControllerDirectory()):
<?php
Zend::loadClass('Zend_Controller_Action');
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
echo 'IndexController::indexAction()';
}
}
?>
The IndexController class handles requests for which the controller is index or for which the indicated controller does not exist, as just explained. The indexAction() method handles requests for which the action is index. Remember that index is the default value for both the controller and the action. If you try a request for /, /index, or /index/index, the indexAction() method is executed (trailing slashes do not alter this behavior). A request for any other resource is going to result in an error.There is another useful method to add to IndexController before continuing. The noRouteAction() method is called whenever a request is made for a controller that doesn't exist. For example, a request for /foo/bar executes noRouteAction() if FooController.php does not exist. However, a request for /index/foo still results in an error, because foo is the action, not the controller. Add noRouteAction() to IndexController.php:
<?php
Zend::loadClass('Zend_Controller_Action');
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
echo 'IndexController::indexAction()';
}
public function noRouteAction()
{
$this->_redirect('/');
}
}
?>
This example uses $this->_redirect('/') to illustrate a possible action to take in noRouteAction(). This causes requests for nonexistent controllers to be redirected to the root document (front page).Now create FooController.php:
<?php
Zend::loadClass('Zend_Controller_Action');
class FooController extends Zend_Controller_Action
{
public function indexAction()
{
echo 'FooController::indexAction()';
}
public function barAction()
{
echo 'FooController::barAction()';
}
}
?>
If you again request /foo/bar, you should see that barAction() is being executed, because bar is the action. Not only can you already support friendly URLs, but you can also do so in a very organized way with just a few lines of code. Cool!You can also create a __call() method to handle requests for undefined actions such as /foo/baz:
Now that you can elegantly handle incoming requests with just a few lines of code, you are ready to continue.
 |
|
 |
|
Tags:
test, php, dd, tutorial, anup uk, shiflet, shiflett, zend, framework, web, site, anirudha, fdffffffffffffff, hello world, httpwwwgooglecom, kjkljlkj, asasas, vinod, chilli, shiflettasshat, mama, papa, chuckoo, sathishan, nithyan, ashokan, unni, divya, pradeep, abcphp, zde, jayesh jose, harro, ronnie, sample, oui, lebicheur, controller, error, d, kontrola, omg lol, 5, 46457, dfsdfdsf, sdfsdf, asd, crazy, booga, should tags be allowed to be added like this, no i dont think so the tags should be extracted from content, dont allow users to add tags or youll see the results, tags are the root of all evil, thi is, cvcxv, ioncube, zend framework, you already see the results, has not printable version, user created tags how lame, pet the monkey, shoutmouthcom, maybe only registered users should be able to add tags, adding another worthless tag to hammer in the points made earlier, this is only way to add new tag, didip is a complete bonehead, rapdwordpresscom, trke olmayan dkman, turkce olmayan dokumanlar, prova, anytagiwant, bump, wtf, transformers more than meets the eye, this seems like a really bad idea for some reason even though the implementation is reasonably cool, bullshitbingo, fukoka, h, lol, if it has tags on it it must be web20 and therefore cool, ooh look ajax, mike was here, gfgf, doogii, kkkk, annoying, prueba, hjkhklhkjhkjhjk, hi i want to have my own tag here too, oo, what an odd idea to have your own tags for unregistered users ajax, my tag too , xminds, soooo bad that everyone can add tags unmanaged , spam , w00000000t, o0, bottledpuppiesforsalecom, ciao mondo, lollerskates, user added tagshow lame, i agree with what other people are saying guests shouldnt be allowed to add tags, i have a penis, fala serio what a bad idea , cccccc, im2combr, a hrefhttpwwwim2combrcriao de sitesa, wow, good, beep, chick, bleep, stix, mix, fix, this, jesuswillsaverockstarstoo, loldongs, i like it, this is dumb, anyone can tag any article go to my portsite bitchescom, testitest, oh nos its a tarp, hmmm, lets try, what, someone really ought to clean up these tags its a waste of space and an eyesore
Tags Help
Tags are keywords associated with a web page that help classifying information. You can find a good explanation here.
To add one or more tags to this page, simply enter them below (separate them with a comma) and hit enter or click on the "Go" button.
|
|
 |
|
 |
|
Index
|