# Pram3 **Repository Path**: no2key/pram3 ## Basic Information - **Project Name**: Pram3 - **Description**: A simple and useful PHP framework. 简单够用的PHP框架,改良版,需要PHP5.3+ - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: http://www.pram3.com - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2013-12-19 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Pram3 Pram3是个简单够用的PHP框架,需要PHP5.3以上版本。框架代码放在vendor/Pram目录下, vendor/Utils是一些辅助函数,vendor/Adapters是连接第三方类库使其更易用的适配器。 # 安装 下载整个Pram3项目,安全起见,请将public设置为网站根目录,nginx下的网站设置请参考data/php.conf ~~~ log_format sess '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" "$var_sessid"'; server { listen 80; server_name example.com ~^(?[a-z0-9]+)\.example\.com$; if ($subdomain = "") { set $subdomain "www"; } root /var/www/$subdomain; index index.html index.php; #charset utf-8; userid on; userid_domain example.com; set $var_sessid "-"; if ($http_cookie ~* "uid=(\S+)(;.*|$)") { set $var_sessid $1; } access_log /var/log/nginx/example.access.log sess; try_files $uri $uri/ /index.php$request_uri; location ~ \.(svn|git|hg|bzr|cvs) { return 404; } location ~ \.(css|js|jpg|png|gif|ico|swf|pdf|docx|xlsx|zip|rar|apk|ipk) { access_log off; expires 30d; } location ~ \.(php|asp) { #auth_basic "Restricted"; #auth_basic_user_file authes/example; #fastcgi_pass 127.0.0.1:9000; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; } } ~~~ # 快速入门 ## 入口文件 在入口文件public/index.php中需要设置APP_ROOT目录,它是整个项目根目录,而不是其下的网站根目录public, 其次要包含vendor/Pram/boot.php文件,它是框架的启动文件,boot.php会包含框架核心文件vendor/Pram/core.php。 而core.php会根据需要加载其他框架文件,或执行配置中指定的其他文件加载方式。 ~~~ boot(); route('/', function() { return 'Hello World!'; }); run($app->root); ~~~ ## 网站应用和应用配置 app()函数会产生\Pram\Application的唯一单例,它的boot()方法的仅有的一个参数就是应用配置。 应用配置是含有constants、parameters两个元素的关联数组,而这两个元素本身也是关联数组。 我们一般把配置写在一个单独文件,并放在网站目录之外无法通过浏览器直接访问的地方,但同时也要在文件开头阻止非法访问。 ~~~ '', 'template_source_dir' => APP_ROOT . DS . 'templates', 'template_compiled_dir' => APP_ROOT . DS . 'runtime' . DS . 'compiled', 'scan_dirs' => array( APP_ROOT . DS . 'vendor' . DS . 'plugins', APP_ROOT . DS . 'protected' . DS . 'entities', ), 'PDO' => array( 'dsn' => 'mysql:host=localhost;charset=utf8', 'options' => array( \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, ), ), ); ~~~ 在public/index.php中换一种方式载入应用配置 ~~~ boot(include APP_ROOT . DS . 'settings.php'); ~~~ ## 自动加载 TODO ## Route 路由 Pram3最大亮点是路由,它会根据当前的URL加载和解析少许文件,对于PHP这种不能跨进程共享对象的语言来说, 这样做的好处多多。否则每次boot过程中加载大量无关代码文件,只会生生拉低PHP的执行速度。 ~~~ /', function($name) { return 'Hello ' . $name; }, array('GET')); # 第二种方式,View类 route('/', 'WeekdayView', array('GET', 'POST')); class WeekdayView extends \Pram\View { //TODO: get() post() } ~~~ ## View 控制器/视图 ~~~ event->connect( Event::SIGNAL_AFTER_REQUEST, null, function($view, $request, $args) { $access = $request->getMethod() . ' ' . $request->getURL(); app()->logger()->info($access, $request->post()); } ); } public function get() { return array('weekday' => date('l')); // 返回JSON } public function post() { return $this->get(); } } ~~~ ## Logger 日志 ~~~ info('The time is ' . $today . ' now', date_parse($today)); ~~~ ## Cache 缓存 ~~~ retrieve('footer', function () { return ''; }, 600); ~~~ ## Redis 内存数据库 ~~~ add('admin'); $pool->add('bob'); $pool->add('candy'); echo $pool->has('david') ? 'The name "david" is used !' : ''; ~~~ ## Templater 自带模板引擎 TODO