Home  >  Article  >  PHP Framework  >  Take you to learn the swoole framework in three minutes

Take you to learn the swoole framework in three minutes

coldplay.xixi
coldplay.xixiforward
2021-04-29 18:02:295035browse

Take you to learn the swoole framework in three minutes

Preface

I’ve been learning about swoole recently, so I’ll write down some thoughts. There are many open source frameworks based on swoole. The advantages and disadvantages can be found in Baidu. If websockets and consumer queues are used, it is a good choice to choose a framework based on swoole. Well, without further ado, Wu Daxian will introduce a simple introduction to swoole

Recommended (free):swoole

##Text

swoole has two parts. One is a PHP extension, developed in C, which is the core. The other is a framework, like Yii, TP, and Laravel, which is written in PHP code.

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.

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$php- >cache$php->tpl$php->model$php->mvc$php->plugin
<?php 
/* 
导入config.php文件,这是调用框架必须的第一步 
config.php会载入基本配置选项,和基本函数,并生成全局接口变量$php 
在代码的任何位置处,都可以通过global $php来引用全局接口对象 
*/
require(&#39;config.php&#39;); 
$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'); 
?>
Database interface
Cache system interface
Smarty template engine interface
Call the Model object interface
MVC structure data
Plug-in system interface
Directory specification:

Assume the root directory is $ROOT.

$ROOT/apps

$ROOT /apps: Application code, the code in this directory is public, including classes, configurations, templates, controllers, Models, 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 direct http access.

Ø $ROOT/apps/controllers Web application controller class code

Ø $ROOT/apps/models Data model encapsulation class code

Ø $ROOT/apps/ configs configuration file, access the

Ø ROOT/apps/classes class library through $php->config['db']['master'], where all user-defined classes are stored, which must comply with psr -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: such as 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. An array must be returned in db.php, and the 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 single entry file of the web website can be placed directly in the root directory, or a separate directory can be created for storage, such as $ROOT/webroot/index.php

$ROOT/server.php

Server program startup entrance.

ControllerController

Using swoole's MVC management, the controller class must comply with the following specifications

² 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 Take you to learn the swoole framework in three minutes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete