Yii Framework Documentation

repository·master·Indexed 26 days ago

https://github.com/yiisoft/yii

Documentation for the Yii framework, including installation guides, system requirements (PHP 5.1.0+), and usage of the yiic command-line tool for scaffolding applications, managing database migrations, extracting translation messages, and generating controllers. Also includes details on the console-normalizer utility for fixing window.console inconsistencies in older browsers like IE 8 and 9.

Tokens
7.2K
Snippets
19
Records
43
Agent score
88%

What's inside Yii

  1. Console Normalizer compatibility and fixes

    master

    The Console Normalizer utility provides the following fixes and compatibility improvements:

    • window.console availability: Ensures console is available in IE 8/9 and Chrome even if the developer tools/console are closed.
    • Method Invocation: Fixes issues where console.log.apply(), console.info.apply(), etc., fail in IE 8 (Object error) and IE 9 (Method Invocation error).
    • Grouping: Implements console.group() and console.groupEnd().
    • Timing: Implements console.time() and console.timeEnd().
    • Function.prototype.bind: Provides a compatible implementation of .bind() for IE 8.

    Compatibility: Works with everything after IE 5.5.

  2. Use Console Normalizer to fix IE console issues

    master

    Include the normalizeconsole.min.js script in your HTML to normalize the window.console object. This utility ensures that the console is available even if it is closed in IE 8/9 and Chrome, and it provides support for advanced logging features like grouping, timing, and method invocation via .apply().

    <script src="normalizeconsole.min.js"></script>
    <script>
        console.group('Play around');
        console.time('timer');
    
        console.log('hello');
    
        console.log.apply(console, ['one', 'two', 'three']);
    
        console.info();
        console.warn();
        console.error();
    
        console.timeEnd('timer');
        console.groupEnd();
    </script>
  3. View Yii documentation offline

    master
    You can view the framework documentation offline by pointing your webserver's webroot to the docs/ directory of the repository. This directory contains "The Definitive Guide to Yii" and "The Yii Blog Tutorial".
  4. Create a new Yii application skeleton using yiic

    master

    Yii provides a command-line tool called yiic to scaffold a skeleton web application.

    1. Navigate to the framework directory within your Yii installation.
    2. Run the webapp command followed by the desired destination path.

    Example for Linux:

    cd YiiPath/framework
    ./yiic webapp ../testdrive

    Example for Windows:

    cd YiiPath\framework
    هاyiic webapp ..\testdrive

    Once created, the application can be accessed at http://hostname/YiiPath/testdrive/index.php.

    # Linux
    cd YiiPath/framework
    ./yiic webapp ../testdrive
    
    # Windows
    cd YiiPath\framework
    هاyiic webapp ..\testdrive
  5. Install the Yii framework

    master

    To install Yii, unpack the release files into a web-accessible directory on your server. Ensure the following directory structure is present after unpacking:

    • demos/: Demo applications
    • framework/: Core Yii framework source files
    • requirements/: Requirement checker tool
    • CHANGELOG: Release history
    • LICENSE: License information
    • README: Documentation
    • UPGRADE: Upgrade instructions
  6. Check Yii system requirements

    master

    Yii requires a web server supporting PHP 5.1.0 or above. It has been tested with Apache on Windows and Linux. To verify if your environment meets the necessary requirements, run the requirement checker by navigating to the following URL in your browser (replace YiiPath with your actual installation path):

    http://hostname/YiiPath/requirements/index.php

  7. Create a syntax highlighter using XML and Text_Highlighter_Generator

    master

    To add support for a new language, you do not need to write PHP code manually. Instead, define the syntax highlighting rules in an XML file using <block> and <region> elements, then use Text_Highlighter_Generator to generate the PHP class.

    XML Rule Concepts

    • Block: A portion of text matching a regular expression, highlighted with a single color (e.g., a keyword).
    • Region: Defined by a start and end regular expression. Regions can contain other blocks or regions.
    • Contained: Attributes contained="yes" or never-contained="yes" control whether elements can appear at the top level or only inside regions.
    • Color Groups: Chunks of text assigned to the same color group. In HTML output, these correspond to CSS classes.

    Generating the Class via PHP

    <?php
        require_once 'Text/Highlighter/Generator.php';
        $generator = new Text_Highlighter_Generator('php.xml');
        $generator->generate();
        $generator->saveCode('PHP.php');
    ?>
  8. Highlight text with Text_Highlighter

    master

    To highlight code, create a highlighter instance using the factory method, attach a renderer, and call highlight() with the source code string.

    Basic Workflow

    1. Create highlighter: Text_Highlighter::factory('language_name').
    2. (Optional) Set a renderer: $hl->setRenderer($renderer).
    3. Highlight: $hl->highlight($source_code).

    Note: If no renderer is set, the HTML renderer is used by default. You can pass renderer options directly to the factory method as the second argument.

    require_once 'Text/Highlighter.php';
    require_once 'Text/Highlighter/Renderer/Html.php';
    
    $options = array(
        'numbers' => HL_NUMBERS_LI,
        'tabsize' => 8,
    );
    
    // Option 1: Explicitly setting the renderer
    $renderer = new Text_Highlighter_Renderer_HTML($options);
    $hl = Text_Highlighter::factory('php');
    $hl->setRenderer($renderer);
    
    // Option 2: Passing options directly to factory (uses default HTML renderer)
    // $hl = Text_Highlighter::factory('php', $options);
    
    $html = $hl->highlight(file_get_contents('example.php'));
  9. Configure the `controller` command template path

    master
    You can customize the directory containing the templates used by the controller command by setting the templatePath property. If this path is set, the command will use the views from this directory. If a specific view is missing in your custom directory, the command will fall back to the default framework views located at YII_PATH/cli/views/shell/controller.
  10. Configure CrudCommand options

    master

    When using the CrudCommand (via configuration or extension), you can customize the following properties:

    • templatePath: The directory containing templates for CRUD commands. If not set, it defaults to framework/cli/views/shell/crud. If a custom path is provided and some views are missing, the command falls back to the default views.
    • functionalTestPath: The directory for generating functional test classes.
      • Defaults to protected/tests/functional.
      • Set to false to prevent the generation of functional test files.
    • actions: A list of actions to be created. Each action must correspond to a template file with the same name. The default actions are:
      • create, update, index, view, admin, _form, _view, _search
  11. Configure MigrateCommand properties

    master

    You can customize the behavior of the MigrateCommand using the following properties:

    PropertyTypeDefaultDescription
    migrationPathstringapplication.migrationsPath alias for the directory storing migrations.
    migrationTablestringtbl_migrationName of the table tracking applied migrations.
    connectionIDstringdbApplication component ID for the database connection.
    templateFilestringnullPath alias to a custom template for new migrations.
    defaultActionstringupThe default action if none is provided.
    interactivebooleantrueWhether to prompt for confirmation. Set to false for automated tasks.