安装Zend Framework

来源:互联网 发布:java待遇 编辑:程序博客网 时间:2024/05/17 02:17

this is my understanding now , there maybe some incorrectness in it , but i will be back to verify them soon after i find them.

first time to install the new Zend Framework,it seem really intermediate at first with all the tools: git, composer..., but i saw somewhere else say all you have to do is just download the Zend library and throw it in the php include_path dir , so i spend two days to figure it out what it takes to install the Zend Framework, here is what i found so far, stay tuned , it just get started.

bottom line, you can just throw the Zend library and with some code , you are ready to go, no git, no composer and no complicate classes.

all the composer did for you is set up the auto_load function,the custom auto_load function has following rules:

1 if it is in the zend namespace it will look in to the 'Zend' library(in the default case it is thevendor/zenframework/zendframework/library)

2 else it will look in a fallback dir you provided

3 else it will look in the php inlcude_path

what we want to achieve is let Zend look in the php include_path,to allow this we have to set up the our own autoload function. here is the tricky part , usually we wil provide the class name like this someNamespace_somesubNamespace_classname,but in the  index.php you can see that it use the "Zend\Mvc\Application" syntax, we have to have some code to counter this ,here is a custom autoload function that works(i copy most of the code from the code that is generated by composer):

function myownClassloader($class){ 10         if (false !== $pos = strrpos($class, '\\')) { 11             // namespaced class name 12             $classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($clas    s, 0, $pos)) . DIRECTORY_SEPARATOR;  13             $className = substr($class, $pos + 1); 14         } else { 15             // PEAR-like class name 16             $classPath = null; 17             $className = $class; 18         } 19      20         $classPath .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.    php'; 21  22         if($file = stream_resolve_include_path($classPath)){ 23                 include($file); 24         } 25 }
 now all you have to do is register the function with the spl_autoload_register() function and you are ready to go .

notes:

1 most of the custom autoload function is dealing with path

2 here is the full code of the modified /public/index.php

<?php/** * This makes our life easier when dealing with paths. Everything is in the php include_path now */// Setup autoloadingchdir(dirname(__DIR__)); //we still need to find the config file "config/application.config.php"function myownClassloader($class){if (false !== $pos = strrpos($class, '\\')) {            // namespaced class name            $classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)) . DIRECTORY_SEPARATOR;            $className = substr($class, $pos + 1);        } else {            // PEAR-like class name            $classPath = null;            $className = $class;        }        $classPath .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';if($file = stream_resolve_include_path($classPath)){include($file);}}spl_autoload_register('myownClassloader');