RubyPHP:支持swoole和普通CLI,使用Thinkphp語法規則

Github地址:https://github.com/jiangliuer326442/rubyPHP

RubyPHP是一個輕量級的PHP MVC框架,支持Swoole和CLI兩種運行模式。

什麼是swoole?

swoole是PHP的一個插件,點擊以下鏈接安裝,區別於普通PHP的地方在於:類的實例化不必等到每次請求的時候創建,mysql和redis使用長連接,不必每次請求的時候再去創建。在高併發的場景下可與java相媲美。

開始旅程

  1. 引入框架 將RubyPHP文件夾放在服務器上的任意目錄,如:/data/user/fanghailiang/swoole/文件夾中,在項目入口文件引入框架 index.php:
define("FRAMEWORK","/data/user/fanghailiang/swoole/RubyPHP/");
require(FRAMEWORK."index.php");
  1. 配置路由 路由是設置url路徑和控制器中的模塊、方法的對應關係,default代表根路由,RubyPHP支持正則路由 config/route.php
$config['route'] = array(
	'default'						=> 'default/welcome:index',
);
  1. 配置數據庫連接 之所以配置兩個mysql連接,是因爲本框架支持mysql主從分離 config/mysql.php
global $config;

$config['mysql'] = array(
	'prefix' => '',
	'master' => array(
		'host' => '',
		'port' => '',
		'username' => '',
		'password' => '',
		'database' => '',
	),
	'slaver' => array(
		'host' => '',
		'port' => '',
		'username' => '',
		'password' => '',
		'database' => '',
	),
);

config/redis.php

global $config;
$config['redis'] = array(
	//'url路徑'=>'模塊路徑:方法'
	'enable' => true, //使用redis緩存
	'host' => '127.0.0.1', //主機
	'port' => 6379, //端口號
	'password' => '', //密碼
	'expire' => 300, //過期時間(秒)
	'database' => 7, //redis緩存使用的數據庫
);
  1. 創建控制器 controller/default/welcome.php 不用寫註釋了,完全是ThinkPHP的語法,唯一改動的是I方法變成了$this->I的形式
class Welcome extends Controller {
	public function index(){
		S("age", 12, 120);
		$age = S("age");
		$info = M("admin_users")->find($this->I("id"));
		$this->assign("age", $age);
		$this->assign("user", $info['realname']);
		$this->assign("title", "RubyPHP");
		$this->display("index");
	}
}
  1. 創建視圖 使用Smarty的語法規則,分界符默認爲{:} vide/index.html
{:$user}你好,您的年齡是{:$age},歡迎來到{:$title}

項目運行

  1. swoole運行 命令行輸入:php index.php -p 9501,-p指定使用的端口 nginx 設置反向代理
server                                                                                                                                                                      
    {   
        listen 80; 
        #listen [::]:80;
        server_name swoole.fanghailiang.companyclub.cn ;

        include none.conf;
    
        location / { 
            proxy_pass http://localhost:9501;
        }   
    }

url輸入 http://swoole.fanghailiang.companyclub.cn/?id=4即可訪問 運行效果圖 2. cli模式運行 吧原來的控制器移到scripts目錄下 controller/scripts/default/welcome.php

class Welcome extends Controller {
	public function index(){
		$this->assign("title", "RubyPHP");
		$this->display("index");
	}
}

命令行運行:php index.php default/welcome:index

相關問題

  1. 如何在後臺運行? nohup php index.php -p 9501 &
  2. mysql或redis長連接被中斷如何處理? 解決思路是捕捉長連接失敗的異常,然後重新建立長連接。待後續版本補足這個缺點。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章