The swoole extension itself provides web server functionality and can replace php-fpm. And if you only use the swoole framework, it can run in web servers such as nignx and apache like other PHP frameworks.
The swoole framework, like the PHP framework, is suitable for web development. The swoole extension provides a lower-level server communication mechanism, which can use UDP, TCP and other protocols, not just http.
The installation method is also different. The swoole extension is installed like other PHP extensions. You can use pecl or compile and install it. The swoole framework can be installed after being introduced with composer, or you can manually include/require after downloading the source code.
In addition, the swoole framework relies on the swoole extension and is an application example of the swoole extension.
Framework-Swoole extension-Swoole Document Center http://wiki.swoole.com/wiki/page/p-framework.html
Swoole extension is the foundation. Based on swoole extension, you can do Develop multiple frameworks, not just web frameworks.
Recommendation (free): swoole
The framework uses a unique interface object mechanism.
The first step in calling the framework, require('config.php'); must first include config.php, and then the $php object will be generated. If it is in Controller, Model, or View, call it through $this->swoole. If it is in a function or other included program, it is referenced through global $php.
$php->db | Database interface |
$php->cache | Cache system interface |
##$php-> tpl | Smarty template engine interface |
Call the Model object interface | |
MVC structure data | |
Plug-in system interface |
##<?php /* 导入config.php文件,这是调用框架必须的第一步 config.php会载入基本配置选项,和基本函数,并生成全局接口变量$php 在代码的任何位置处,都可以通过global $php来引用全局接口对象 */require('config.php'); $res = $php->db->query('select * from test_table'); //执行SQL语句,得到一个查询的结果,查询结果,可以获取数据 $res->fetch(); //获取单条数据。是字段-值,组成的关联数组。 $res->fetchall(); //获取全部 $data = array(); $data['title'] = 'hello wolrd!'; $data['author'] = 'me'; $php->db->insert($data,'test_table'); //将关联数组按照键值对应转为字段-值对应,插入到数据库表test_table中。 //insert into test_table(title,author) values('hello wolrd!','me') /* $php->db->delete() 删除数据 $php->db->update() 更新数据 具体请参考Database类 *//* 模板操作,内置smarty模板引擎 */$php->tpl->assign('title','hello world!'); $php->tpl->display('index.html'); ?> Copy after login Directory specificationAssume the root directory is $ROOT. $ROOT/apps##$ROOT /apps : Application code, the code in this directory is public, including classes, configurations, templates, controllers, Model, etc. Static files such as js, css, jpg, html, etc. must not be placed in this directory. They must all be .php files. This directory does not allow http direct access. ##Ø$ROOT/apps/controllers Web application controller class code Ø$ROOT/apps/models Data model encapsulation class code Ø$ROOT/apps /configs configuration file, access it through $php->config['db']['master']##Ø ROOT/apps/classes class library, where all user-defined classes are stored, must comply with thepsr-0 specification, the file name must be {class name}.php, and the top-level namespace must be App##Ø $ROOT/apps/templates Template file directory
² Namespace: For example, new App\Hello\Test class will be mapped to $ROOT/apps/classes/Hello/ Test.php² Configuration file: such as $php->config['db']['master'] or Swoole ::getInstance()->config['db']['master'] will be mapped to the$ROOT/apps/configs/db.php file, db.php must return an array, key is master. ##² Data model: model('UserInfo') or $php- >model->UserInfo will be mapped to $ROOT/apps/models/UserInfo.php##$ROOT/ static Static file directory, such as js, css, jpg, html, etc.$ROOT/index.php The web website single entry file can be placed directly in the root directory, or Create a separate directory for storage, such as $ROOT/webroot/index.php##$ROOT/server.php Server program startup entry.ControllerControllerUsingswoole's MVC management, the controller class must comply with the following specifications² The code is placed in the apps\controllers directory ² The first letter of the class name must be capitalized² Must inherit from Swoole\Controller
|
The above is the detailed content of Detailed explanation of swoole framework quick start. For more information, please follow other related articles on the PHP Chinese website!