Приклад #1 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/ // Plugins
Set the web server DocumentRoot to the
public directory, so that only the public
assets are accessible from the web.
Приклад #2 A multi-module Application directory layout
+ public/
+ conf/
+ application/
+ modules/
+ Index/ //default module
+ controllers/
+ views/
+ Admin/ //another module
+ controllers/
+ views/
+ library/
+ models/
+ plugins/
- Bootstrap.php
In 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.
Приклад #3 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();
?>Приклад #4 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",
)
}
Приклад #5 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
}
}
?>Приклад #6 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=bar
Приклад #7 Application 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);
?>Приклад #8 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";
}
}
?>Приклад #9 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>Приклад #10 Run the Application
Point the browser at the configured domain
(e.g. http://www.example.com) and the response
will look like:
Поданий вище приклад виведе щось схоже на:
<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.