迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
EZ Framework for PHP[中文文档] [English README]
关于EZ是一个php mvc框架,它轻量,美丽而性感。 为什么使用EZ? 因为它EASY! :) 维基这里是一个使用EZ搭建的应用示例,你可以参照这个项目来使用EZ。 oschina: http://git.oschina.net/xiaozhuai/ez_app github: https://github.com/xiaozhuai/ez_app Hello World让我们从最简单的Hello World开始 1. 创建一个apache <IfModule mod_rewrite.c> RewriteEngine On #ignore if it's a file RewriteCond %{REQUEST_FILENAME} !-f RewriteRule .* index.php</IfModule> 这个文件只是一个重写规则,让所有的请求经过index.php来处理,除了静态文件 你可以找到其它环境下的配置,像nginx什么的,google it. 2. 克隆EZ到目录 3. 在项目根目录创建一个入口文件 require_once __DIR__ . "/vendor/ez/autoload.php"; //include ez autoloadEZ()->init(__DIR__); //init framework in cur dirEZ()->mvc( //define the mvc path __DIR__ . "/models", __DIR__ . "/views_php", __DIR__ . "/controllers");EZ()->run(); //let's go 4. 创建 5. 在 class IndexController extends EZController //all Controller must extends EZController{ public function index(){ echo "Hello world"; }} 完成了, 访问 http://localhost/ 试试吧 :) 配置 和 函数Hello World 很简单不是吗?来看一些高级货。 找到 ez_app 项目中的 require_once __DIR__ . "/vendor/ez/autoload.php";EZ()->session(true); //whether enable session supportEZ()->init(__DIR__); //init framework in cur dirEZ()->errView(__DIR__ . "/views_php/err.phtml"); //define the err page view tplEZ()->config( //load configs include __DIR__ . "/conf/main.config.php", include __DIR__ . "/conf/db.config.php", include __DIR__ . "/conf/php_view_engine.config.php");EZ()->mvc( //define the mvc path __DIR__ . "/models", __DIR__ . "/views_php", __DIR__ . "/controllers");EZ()->library(__DIR__ . "/lib"); //define the user class lib, will be automatically includedEZ()->preRun(function(){ assert(1+1==2); //do something before run});EZ()->run(); //let's go 正如你看到的,几乎所有的调用都是形似 这里有一个列表,你可以在
所有这个函数(方法)都会返回一个实例,它们都是单例对象。 对于配置文件,一个合法的配置文件内容如下 return array( "WEB_ROOT" => "/", "VIEW_ACTION_HYPHEN" => "/",); 注意! 字母全部大写的配置项是内置的,如果你想扩展一些配置一遍在应用中使用的话,请使用小写字母以区分。 你可以像例子中一样同时加载多个配置文件 所有内置的配置项如下, 或者你可以在 /** * define the static path pf the project, change this by call EZ()->init() */public $PROJECT_PATH = "";/** * define the root of the project, for example "/var/www/myproject", * then it should be set to "/myproject" */public $WEB_ROOT = "/";/** * define the ext name of view */public $VIEW_EXT = "phtml";/** * define the hyphen of view, for example, a controller "Home" has an action called "index", if set this to '.', then * view file name should be Home{$VIEW_ACTION_HYPHEN}index.{$VIEW_EXT} (Home.index.phtml). * if you want to put all action view under a child dir, then you can just set this to '/'. */public $VIEW_ACTION_HYPHEN = "/";/** * define the view engine of view renderer, avaliable engines: php, smarty, twig, haml_php, haml_twig */public $VIEW_ENGINE = "php";public $PDO_DB_DSN = "";public $PDO_DB_USER = "root";public $PDO_DB_PWD = "";public $PDO_DB_OPTIONS = array();public $MONGO_DSN = "";public $MONGO_DBNAME = ""; 由于EZConfig是单例,所以它是全局的。 如果你想要存储一些全局变量,我建议你使用 路由 和 控制器內建路由EZ框架的路由行为类似于其它一些框架,像 但不是完全相同,至少它很EZ。 EZRouter 将会匹配Controller目录下的类和方法作为控制器和行为 有 3 种情景 1. 对于url file path 是 它总是会route到
2. 对于 url file path 是 首先他会在 如果 如果以上情况都不成立,会抛出一个controller不存在的异常。 3. 当然,这里其实并没有第三种情形 :) 自定义路由你可以制定一个如下的路由规则: return array( "user/:id" => "user", "user/info/:name/:age/:sex" => "user/info"); 具体可查看 模型EZ支持pdo(mysql, sqlite, 等等) 和 mongodb,你可以自己扩展更多的model,通过继承 为什么不支持orm?因为我不喜欢orm, :) 性能很低下不是吗,当然如果需要的话,那就自己扩展吧。 视图 和 视图引擎EZ支持一些流行的视图引擎,而且你也可以自己扩展更多的视图引擎,所有这一切都是EZ的 内置的视图引擎有
你可以参照 或者你可以扩展其它视图引擎,参照例子 这是所有你需要做的事情来扩展一个支持markdown和php混合的视图引擎 require_once __DIR__ . "/vendor/ez/autoload.php";EZ()->session(true); //if enable session supportEZ()->init(__DIR__); //init framework in cur dirEZ()->config( //load configs include __DIR__ . "/conf/main.config.php", include __DIR__ . "/conf/db.config.php", include __DIR__ . "/conf/php_markdown_view_engine.config.php");EZ()->mvc( //define the mvc path __DIR__ . "/models", __DIR__ . "/views_php_markdown", __DIR__ . "/controllers");EZ()->library(__DIR__ . "/lib"); //define the user class lib, will be automatically includedEZ()->viewEngine(new Parsedown()); //the view engine instance/** * $engine is set by EZ()->viewEngine($parsedown); * $vars * $path is realpath path (static path) of view tpl file */EZ()->registerViewEngine("php_markdown", function ($engine, $vars, $path){ /** * first, render tpl just like phtml */ ob_start(); foreach ($vars as $key => $value) ${$key} = $value; require_once $path; $markdown = ob_get_contents(); ob_clean(); /** * then, use Parsedown to transfer markdown to html */ echo $engine->text($markdown);});EZ()->run(); //let's go EZ? So enjoy! :) ![]() |
请发表评论