Quick Start
Set the web server DocumentRoot to the public directory, so that only the public assets are accessible from the web.
Examples
Quick Start
A classic Application directory layout
- .htaccess // Rewrite rules
+ public/
| - index.php // Application entry
| + css/
| + js/
| + img/
+ conf/
| - application.ini // Application config
- application/
- Bootstrap.php // Bootstrap
+ controllers/
- Index.php // Default controller
+ views/
|+ index/
- index.phtml // View template for default action
+ library/ // Libraries
+ models/ // Models
+ plugins/ // PluginsSet the web server DocumentRoot to the public directory, so that only the public assets are accessible from the web.
A multi-module Application directory layout
+ public/
+ conf/
+ application/
+ modules/
+ Index/ //default module
+ controllers/
+ views/
+ Admin/ //another module
+ controllers/
+ views/
+ library/
+ models/
+ plugins/
- Bootstrap.phpIn a multi-module application, each module has its own controllers and views directory under application/modules/<ModuleName>/. The registered modules are declared in application.modules.
Entry
index.php in the public directory is the only way into the application, you should rewrite all requests to it (you can use .htaccess in Apache + mod_php, or the equivalent in your web server, see below).
<?php
define("APPLICATION_PATH", dirname(dirname(__FILE__)));
$app = new Yaf_Application(APPLICATION_PATH . "/conf/application.ini");
$app->bootstrap() //call bootstrap methods defined in Bootstrap.php
->run();
?>Rewrite rule
#for apache (.htaccess)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php
#for nginx
server {
listen 80;
server_name domain.com;
root document_root;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
}
#for lighttpd
$HTTP["host"] =~ "(www.)?domain.com$" {
url.rewrite = (
"^/(.+)/?$" => "/index.php/$1",
)
}Bootstrap
Any method of the Bootstrap class whose name begins with _init is automatically called, in definition order, by Yaf_Application::bootstrap. Each such method receives the Yaf_Dispatcher instance as its argument. Methods with other names are not called automatically.
<?php
class Bootstrap extends Yaf_Bootstrap_Abstract
{
public function _initConfig(Yaf_Dispatcher $dispatcher)
{
// called first
}
public function _initPlugin(Yaf_Dispatcher $dispatcher)
{
// called second
}
public function _initRoute(Yaf_Dispatcher $dispatcher)
{
// called third
}
}
?>Application config
application.ini is the application configuration file. Constants defined in index.php can be used in it, and sections can inherit from one another.
[yaf]
;APPLICATION_PATH is the constant defined in index.php
application.directory=APPLICATION_PATH "/application/"
;product section inherit from yaf section
[product:yaf]
foo=barApplication config as a PHP array
Besides an INI configuration file, a plain PHP array can also be passed to Yaf_Application::__construct as the configuration.
<?php
$config = array(
"application" => array(
"directory" => APPLICATION_PATH . "/application/",
),
);
$app = new Yaf_Application($config);
?>Default controller
In Yaf, the default controller is named IndexController:
<?php
class IndexController extends Yaf_Controller_Abstract
{
// default action name
public function indexAction()
{
$this->getView()->content = "Hello World";
}
}
?>Default view template
The view script for the default controller/action is application/views/index/index.phtml. Yaf provides a small view engine called Yaf_View_Simple, whose templates are plain PHP scripts.
<html>
<head>
<title>Hello World</title>
</head>
<body>
<?php echo $content;?>
</body>
</html>Run the Application
Point the browser at the configured domain (e.g. http://www.example.com) and the response will look like:
The above example will output something similar to:
<html>
<head>
<title>Hello World</title>
</head>
<body>
hello world
</body>
</html>The minimal example above can also be generated with the Yaf code generator (tools/cg/yaf_cg), which ships with the Yaf source repository:
$ cd tools/cg
$ ./yaf_cg -d output_directory [-a application_name] [--namespace]yaf_cg generates a more complete, commented skeleton than the one shown in this tutorial: a public/ entry with rewrite rules, a Bootstrap with configuration, plugin and custom-route setup, an example model, an error controller and view templates. It is a good starting point for a real application.
Custom Routes
Custom routes
Besides the default Yaf_Route_Static route, custom routes can be registered on the Yaf_Router route stack, usually in a _initRoute method of the Bootstrap, using Yaf_Router::addRoute. Routes can also be declared in configuration files and loaded with Yaf_Router::addConfig.
<?php
class Bootstrap extends Yaf_Bootstrap_Abstract
{
public function _initRoute(Yaf_Dispatcher $dispatcher)
{
$router = $dispatcher->getRouter();
// /user/123 -> controller=user, action=index, id=123
$router->addRoute("user", new Yaf_Route_Rewrite(
"/user/:id",
array("controller" => "user", "action" => "index")
));
}
}
?>Plugins
Plugins
A plugin hooks into the dispatch cycle by extending Yaf_Plugin_Abstract and overriding one or more of its six hook methods, then is registered on the dispatcher with Yaf_Dispatcher::registerPlugin, typically in the Bootstrap:
<?php
class UserPlugin extends Yaf_Plugin_Abstract
{
public function routerStartup(
Yaf_Request_Abstract $request, Yaf_Response_Abstract $response)
{
// before routing: inspect or rewrite the raw request URI
}
public function routerShutdown(
Yaf_Request_Abstract $request, Yaf_Response_Abstract $response)
{
// after routing: module/controller/action are known, good for per-route access control
}
public function dispatchLoopStartup(
Yaf_Request_Abstract $request, Yaf_Response_Abstract $response)
{
// before the dispatch loop: one-time setup shared by all dispatches
}
public function preDispatch(
Yaf_Request_Abstract $request, Yaf_Response_Abstract $response)
{
// before each dispatch: filter parameters or redirect before the action runs
}
public function postDispatch(
Yaf_Request_Abstract $request, Yaf_Response_Abstract $response)
{
// after each dispatch: decorate the response or the rendered view
}
public function dispatchLoopShutdown(
Yaf_Request_Abstract $request, Yaf_Response_Abstract $response)
{
// after the dispatch loop: final cleanup, logging or flushing
}
}
class Bootstrap extends Yaf_Bootstrap_Abstract
{
public function _initPlugin(Yaf_Dispatcher $dispatcher)
{
$dispatcher->registerPlugin(new UserPlugin());
}
}
?>