PHP learning materials

Source: Internet
Author: User
Tags autoloader zend framework
PHP learning materials

Http://hi.baidu.com/renxian/blog

 

Http://www.zendchina.net

Posted @ Jacob. Xu read (10) Comments (0) EDIT Bootstrap

Bootstrap, as its name implies, is to tie the shoelaces and point out the preparations made before sending. Since Zend framework 1.8 and later, Zend framework has released zend_tool and zend_application. Therefore, bootstrap has made great adjustments. Basically, you only need to set the value in index. when zend_application is called in PHP, you can directly set the config file, DB, view, and so on. If you need to make special settings, you can extend zend_application_bootstrap_bootstrap in Bootstrap and use the _ init * Resource function to set the settings. Bootstrap will call these _ init * resource functions one by one in order, then execute dispatch. Note the following points:

1. Do not mark the response attribute during initview, so that this Bootstrap will not instantiate response before layout;

2. You can determine the Ajax request header to remove layout, viewrender, errorhandler, and exception output. This frees you from setting it on the Ajax page.

3. After zf1.8, you can set the driver option to lazy load the DB charset of ZF.


Bytes ---------------------------------------------------------------------------------------------------------------

<? PHP

Class Bootstrap extends zend_application_bootstrap_bootstrap

{

Protected $ _ config;

Protected $ _ cache;

Protected $ _ requesttype;


Public Function run ()

{

$ Frontcontroller = zend_controller_front: getinstance ();

$ Frontcontroller-> dispatch ();

}


Protected function _ initautoload ()

{

$ Autoloader = new zend_application_module_autoloader (Array (

'Namespace '=> '',

'Basepath' => application_path,

));

Return $ autoloader;

}


Protected function _ initconfig ()

{

// Config

$ This-> _ Config = new zend_config_ini (application_path. '/configs/config. ini', application_env );

Zend_registry: Set ('config', $ this-> _ config );

Zend_registry: Set ('env', application_env );


// Load application level configure

$ Appconf = new zend_config_ini (application_path. '/configs/APP. conf', application_env );

Zend_registry: Set ('appconf', $ appconf );


If (1 = (INT) $ this-> _ config-> debug-> showpageinfo ){

Zend_registry: Set ('pagestarttime', microtime (true ));

}

}


Protected function _ initdb ()

{

If ($ this-> _ config-> dB ){

$ Db = zend_db: Factory ($ this-> _ config-> dB );


If ($ this-> _ config-> cache-> tablemeta & isset ($ this-> _ cache) {// setup metacache to speed up

Zend_db_table: setdefametametadatacache ($ this-> _ cache );

}


Zend_db_table_abstract: setdefaadapter adapter ($ dB );

Zend_registry: Set ('db', $ dB );

}

}


Protected function _ initview ()

{

// View and layout setup

$ View = new zend_view (Array ('encoding' => 'utf-8 '));

$ View-> addhelperpath (application_path. '/default/views/helpers /');

// $ Viewrendered = new zend_controller_action_helper_viewrenderer ($ view );

// Zend_controller_action_helperbroker: addhelper ($ viewrendered );


Zend_dojo: enableview ($ view );

$ View-> dojo ()-> setdjconfigoption ('parseonload', true)

-> Requiremodule ('dijit. Form. filteringselect ')

-> Requiremodule ('m M. deleredstore ');

// $ View-> addbasepath (realpath ('./templates/default /'));


// If Ajax submit

If (isset ($ _ server ['HTTP _ x_requested_with ']) & $ _ server ['HTTP _ x_requested_with'] = 'xmlhttprequest '){

$ This-> _ requesttype = "ajax ";

} Else {

$ This-> _ requesttype = "html ";

Zend_layout: startmvc (Array (

'Lastyoupath' => application_path. '/layouts ',

'Layout '=> 'layout'

));

}

$ View-> doctype ('xhtml1 _ strict ');

Zend_controller_action_helperbroker: getstatichelper ('viewrenderer')-> setview ($ view );

}


Protected function _ initfrontcontroller ()

{

$ Frontcontroller = zend_controller_front: getinstance ();

$ Frontcontroller-> setcontrollerdirectory (application_path. '/controllers ');

$ Frontcontroller-> setparam ('env', application_env );

If ("ajax" ==$ this-> _ requesttype ){

// Disable the errorhandler plugin

$ Frontcontroller-> setparam ('noerrorhandler', true );

// Disable the viewrenderer helper

$ Frontcontroller-> setparam ('noviewrenderer', true );

}

$ Frontcontroller-> throwexceptions (false );

$ Frontcontroller-> setbaseurl ('/');

// Action helpers

Zend_controller_action_helperbroker: addpath (application_path. '/controllers/helpers ');

}


Protected function _ initsession ()

{

If (isset ($ this-> _ config-> session )){

$ Session = $ this-> _ config-> session;

If (empty ($ session-> save_path )){

$ Session-> save_path = application_path. "/../data/sessions /";

}

}

Zend_session: setoptions ($ this-> _ config-> session-> toarray ());

}


Protected function _ initcache ()

{

// Setup core Cache

If ('true' = $ this-> _ config-> cache-> caching ){

$ Cachefrontendoptions = array (

'Lifetime' => $ this-> _ config-> cache-> lifetime, // cache lifetime of half a minute

'Caching' => $ this-> _ config-> cache-> caching,

'Ic IC _ serialization' => $ this-> _ config-> cache-> automatic_serialization, // This is default anyway

'Ic IC _ cleaning_factor '=> $ this-> _ config-> cache-> automatic_cleaning_factor,

'Logging' => $ this-> _ config-> cache-> logging,

);

Switch ($ this-> _ config-> cache-> backend ){

Case 'memcached ':

$ Memcachedbackendoptions = array (

'Servers' => array (Array (

'Host' => $ this-> _ config-> cache-> memcached-> host,

'Port' => $ this-> _ config-> cache-> memcached-> port,

'Persistent' => $ this-> _ config-> cache-> memcached-> persistent

)));

$ This-> _ cache = zend_cache: Factory ('core', 'memcached', $ cachefrontendoptions, $ memcachedbackendoptions );

Break;

Case 'file ':

Default:

$ Filebackendoptions = array (

'Cache _ dir' => application_path. '/../data/Cache /',

'Read _ control' => true,

'Read _ control_type '=> 'md5 ',

'Hashed _ directory_level '=> '1'

);

$ This-> _ cache = zend_cache: Factory ('core', 'file', $ cachefrontendoptions, $ filebackendoptions );

Break;

} // End of Switch

Zend_registry: Set ('cache', $ this-> _ cache );

}


// Setup page Cache

If ($ this-> _ config-> cache-> page-> caching ){

$ Pagecacheregexpsconfig = array (

'Cache _ with_get_variables '=> true,

'Cache _ with_post_variables '=> true,

'Cache _ with_session_variables '=> true,

'Cache _ with_files_variables '=> true,

'Cache _ with_cookie_variables '=> true

);

$ Pagecachefrontendoptions = array (

'Lifetime' => $ this-> _ config-> cache-> lifetime,

'Debug _ header' => $ this-> _ config-> cache-> page-> debug, // for debugging

'Regexps' => array (

// Cache the whole indexcontroller

'^ (. +)-LC-(. * example .html' => array_merge ($ pagecacheregexpsconfig, array ('tags' => array ('lc '))),

)

);


If (empty ($ filebackendoptions )){

$ Filebackendoptions = array (

'Cache _ dir' => application_path. '/../data/Cache /',

'Read _ control' => true,

'Read _ control_type '=> 'md5 ',

'Hashed _ directory_level '=> '1'

);

}


// Getting a zend_cache_frontend_page object

$ Page_cache = zend_cache: Factory ('page ',

'File ',

$ Pagecachefrontendoptions,

$ Filebackendoptions

);

$ Page_cache-> Start ();

}

}


Protected function _ inittranslation ()

{

// Setup locale

Zend_locale: setdefault ('en'); // fallback locale

$ Locale = new zend_locale ('auto'); // Auto Detection for User Locale

If (isset ($ this-> _ cache )){

Zend_locale: setcache ($ this-> _ cache); // cache locale to speed up

}

Zend_registry: Set ('zend _ locale ', $ locale); // set up application-wide locale


// Setup Zend translate

// $ This-> _ cache = zend_cache: Factory ('page', 'file', $ frontendoptions, $ backendoptions );

// Zend_translate: setcache ($ this-> _ cache );

$ Translate = new zend_translate ('gettext', '../data/locales/', null, array (

'Scan' => zend_translate: locale_directory,

'Disablenotices' => true

));

Zend_registry: Set ('zend _ translate', $ translate );

// Zend_validate_abstract: setdefaulttranslator ($ translate );

Zend_form: setdefaulttranslator ($ translate );

}


Protected function _ initroutes (){

// $ Router = new zend_controller_router_rewrite ();

// $ Router-> addconfig ($ appconf, 'routes ');

}

}

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.