首页
下载
文档
社区
视频
捐赠
源代码
赞助商
AOT 编译器
AI 助理
商业产品
PHP AOT 原生编译器
Swoole-Compiler 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
swolle http mcv设计问题
### 问题描述 使用 swoole http 的问题 就是改变了 控制器文件 不重启 怎么还能 才能让类正常使用 比如 index\controller\index类里面的index方法 调用返回 333 然后我修改了 这个文件 让它index方法 返回444 在不重启服务的情况下 怎么 才能释放这个 命名空间 ### Swoole版本,PHP版本,以及操作系统版本信息 Swoole 4 php 7 centos7 ### 相关代码 ```php //自动加载类 $class_name=str_replace('\\',"//",$class_name); $file_path= root_dir.DS.$class_name.".php"; if (is_file($file_path) && file_exists($file_path)) { return include($file_path); } $file_path= APP.DS.$class_name.".php"; if (is_file($file_path) && file_exists($file_path)) { include($file_path); } return false; ``` ```php //index.php 文件 第一次加载 namespace index\controller; use core\controller; class index extends controller{ public function __construct(){ parent::__construct(); } public function index(){ return json_encode(array("a11"=>"333")); } public function index2(){ $this->assign("home","index12345621方法"); } } ``` ```php //index.php 文件 希望 第二次加载 但是应为已经声明了所以不会再次加载 namespace index\controller; use core\controller; class index extends controller{ public function __construct(){ parent::__construct(); } public function index(){ return json_encode(array("a11"=>"444")); } public function index2(){ $this->assign("home","index12345621方法"); } } ``` ```php //判断文件是否存在 if (file_exists($controller_file)) { echo 'get_declared_classes !'; var_dump(get_declared_classes ()); $class_namespacename = "\\" . $module_name . "\\".controller."\\" . $class_name;//模块,控制器,类名称 $class_obj = new $class_namespacename(); if (method_exists($class_obj, $function_name)) { $http_data=$class_obj->$function_name(); } else { $http_data = "模块/控制器/方法->" . $module_name . '/' . $controller_name . '/' . $function_name . "不存在!"; } //判断试图文件是否存在 存在则使用试图 if(empty($http_data) && file_exists($view_file)){ $http_data = $smarty->fetch($view_file); } $this->swoole_response->header("Content-Type", "text/html; charset=utf-8"); $this->swoole_response->end($http_data); unset($class_obj);//使用完成后释放对象 } else { $this->swoole_response->header("Content-Type", "text/html; charset=utf-8"); $http_data = '<h1 style="text-align: center; margin: 50px;">404 文件丢失</h1>'; $this->swoole_response->end($http_data); } ``` ### 你期待的结果是什么?实际看到的错误信息又是什么?
发布于6年前 · 2 次浏览 · 来自
提问
cpz2020
### 问题描述 使用 swoole http 的问题 就是改变了 控制器文件 不重启 怎么还能 才能让类正常使用 比如 index\controller\index类里面的index方法 调用返回 333 然后我修改了 这个文件 让它index方法 返回444 在不重启服务的情况下 怎么 才能释放这个 命名空间 ### Swoole版本,PHP版本,以及操作系统版本信息 Swoole 4 php 7 centos7 ### 相关代码 ```php //自动加载类 $class_name=str_replace('\\',"//",$class_name); $file_path= root_dir.DS.$class_name.".php"; if (is_file($file_path) && file_exists($file_path)) { return include($file_path); } $file_path= APP.DS.$class_name.".php"; if (is_file($file_path) && file_exists($file_path)) { include($file_path); } return false; ``` ```php //index.php 文件 第一次加载 namespace index\controller; use core\controller; class index extends controller{ public function __construct(){ parent::__construct(); } public function index(){ return json_encode(array("a11"=>"333")); } public function index2(){ $this->assign("home","index12345621方法"); } } ``` ```php //index.php 文件 希望 第二次加载 但是应为已经声明了所以不会再次加载 namespace index\controller; use core\controller; class index extends controller{ public function __construct(){ parent::__construct(); } public function index(){ return json_encode(array("a11"=>"444")); } public function index2(){ $this->assign("home","index12345621方法"); } } ``` ```php //判断文件是否存在 if (file_exists($controller_file)) { echo 'get_declared_classes !'; var_dump(get_declared_classes ()); $class_namespacename = "\\" . $module_name . "\\".controller."\\" . $class_name;//模块,控制器,类名称 $class_obj = new $class_namespacename(); if (method_exists($class_obj, $function_name)) { $http_data=$class_obj->$function_name(); } else { $http_data = "模块/控制器/方法->" . $module_name . '/' . $controller_name . '/' . $function_name . "不存在!"; } //判断试图文件是否存在 存在则使用试图 if(empty($http_data) && file_exists($view_file)){ $http_data = $smarty->fetch($view_file); } $this->swoole_response->header("Content-Type", "text/html; charset=utf-8"); $this->swoole_response->end($http_data); unset($class_obj);//使用完成后释放对象 } else { $this->swoole_response->header("Content-Type", "text/html; charset=utf-8"); $http_data = '<h1 style="text-align: center; margin: 50px;">404 文件丢失</h1>'; $this->swoole_response->end($http_data); } ``` ### 你期待的结果是什么?实际看到的错误信息又是什么?
赞
0
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
登录
后参与评论
评论
2020-09-01
鲁飞
能问这个问题,说明你还没有理解Swoole的生命周期。 可以看下文档[reload()](https://wiki.swoole.com/#/server/methods?id=reload)方法的介绍,以及[如何正确的重启服务](https://wiki.swoole.com/#/question/use?id=swoole%e5%a6%82%e4%bd%95%e6%ad%a3%e7%a1%ae%e7%9a%84%e9%87%8d%e5%90%af%e6%9c%8d%e5%8a%a1)
赞
0
回复
2020-09-01
cpz2020
回复
鲁飞
我认为这个是PHP,设计上的问题. 你这个是Swoole,级别的解决方案,属于平滑重启.当然是你们比较专业. 我的想法是,服务器在不重启的情况下改变控制器。 如果,我在编写的时候,控制器输出与预期不符,我就可以直接修改控制器文件。 但是应为框架改成MVC后是常驻内存的 已经声明的类 namespace index\controller; 不能再次声明 目前也没有找到合适的办法去释放掉重新加载。 我可以尝试你说的reload() 但是我觉得不是一个很好的解决方案 如果我检测到文件发生变化后 关闭掉当前进程 那要等下一次 处理才会正常返回
赞
0
回复
2020-09-02
鲁飞
回复
cpz2020
都2020年了,用composer自动加载不香吗? 用Swoole修改了代码就要重启或者停掉重新启动。 fpm下修改就不需要重启服务。
赞
0
回复
2020-09-02
cpz2020
回复
鲁飞
我自己弄的框架 这个不是自动加载的问题 是类被声明后 但是文件改变了 但是我又不想改变他的 命名空间 我先看看 swoole的文档 实在没有解决办法 我就 让他重启吧 反正 能解决问题了
赞
0
回复