Yaf (Yet Another Framework)

repository·master·Indexed 26 days ago

https://github.com/laruence/yaf

A high-performance PHP framework written in C and distributed as a PHP extension. Yaf follows a classic MVC-like structure and includes the yaf_cg code generator for scaffolding application directory structures.

Tokens
1.4K
Snippets
9
Records
11
Agent score
39%

What's inside Yaf

  1. Compile Yaf from source on Linux

    master

    If you need to install Yaf manually on Linux, use phpize and configure to build the extension.

    $/path/to/phpize
    $./configure --with-php-config=/path/to/php-config
    $make && make install
  2. Configure Yaf application directory structure

    master

    A standard Yaf application follows this directory layout. Note that the public directory should be your DocumentRoot to ensure only the entry point is accessible to users.

    - .htaccess // Rewrite rules
    + public
      | - index.php // Application entry
      | + css
      | + js
      | + img
    + conf
      | - application.ini // Configure 
    - application/
      - Bootstrap.php   // Bootstrap
      + controllers
         - Index.php // Default controller
      + views    
         |+ index   
            - index.phtml // View template for default controller
      + library // libraries
      + models  // Models
      + plugins // Plugins
  3. Use the Yaf Codes Generator (yaf_cg)

    master
    The yaf_cg tool is a PHP-based code generator used to scaffold the directory structure and initial files for a Yaf project. You can specify a directory name using the -d flag. Optionally, use the -n flag to generate a namespace example within the scaffolded project.
  4. Configure application via application.ini or PHP array

    master

    You can configure the application using an application.ini file or by passing a PHP array to the Yaf_Application constructor.

    #### Using application.ini
    ```ini
    [product]
    ;CONSTANTS is supported
    application.directory = APPLICATION_PATH "/application/" 

    Using a PHP array

    <?php
    $config = array(
       "application" => array(
           "directory" => application_path . "/application/",
        ),
    );
    
    $app  = new yaf_application($config);
  5. Use Yaf_View_Simple for templates

    master

    Yaf provides Yaf_View_Simple, which allows you to use PHP templates. Variables assigned to the view in the controller (e.g., $this->getView()->content) are available in the template.

    <html
     <head>
       <title>Hello World</title>
     </head>
     <body
       <?php echo $content; ?>
     </body>
    </html>
  6. Initialize Yaf application in index.php

    master

    The index.php file located in your public directory serves as the application entry point. It initializes the Yaf_Application using the configuration file and runs the bootstrap process.

    <?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();
  7. Reference Yaf Codes Generator CLI flags

    master

    The yaf_cg command accepts the following flags:

    • -d <directory_name>: Specifies the name of the directory to be generated (e.g., Sample). This directory will contain application/, conf/, index.php, and readme.txt.
    • -n: Generates a namespace example within the scaffolded project.
    # Example output structure after running 'php yaf_cg -d Sample'
    $ ls ./Sample/
    application/  conf/  index.php  readme.txt
  8. Configure Rewrite Rules for Yaf

    master

    Since index.php is the only entry point, you must rewrite all requests to it using your web server's rewrite engine.

    #### Apache (.htaccess)
    ```conf
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule .* index.php

    Nginx

    server {
      listen ****;
      server_name  domain.com;
      root   document_root;
      index  index.php index.html index.htm;
     
      if (!-e $request_filename) {
        rewrite ^/(.*)  /index.php/$1 last;
      }
    }

    Lighttpd

    $HTTP["host"] =~ "(www.)?domain.com$" {
      url.rewrite = (
         "^/(.+)/?$"  => "/index.php/$1",
      )
    }
  9. Create a default controller

    master

    In Yaf, the default controller is named IndexController. Actions are defined as methods with the suffix Action (e.g., indexAction).

    <?php
    class IndexController extends Yaf_Controller_Abstract {
       // default action name
       public function indexAction() {
            $this->getView()->content = "Hello World";
       }
    }
    ?>