PHP code example of beansir / newx
1. Go to this page and download the library: Download beansir/newx library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
beansir / newx example snippets
namespace app\controllers; // 命名空间必须与应用文件夹名以及应用配置中的应用名称保持一致
use newx\base\BaseController;
class HomeController extends BaseController // 控制器首字母大写并以Controller为后缀,继承框架底层控制器基类
{
public function actionIndex() // 方法名首字母大写并以action为前缀
{
$data = [];
// 方式一 布局视图渲染
$this->layout = 'test'; // 自定义布局文件名,默认layout,也可以目录形式命名,例如:test/test
$output = $this->view('index', $data);
// 方式二 非布局视图渲染
$output = $this->view('index', $data, false);
return $output;
}
}
namespace app\models;
use newx\orm\base\Model;
class UserModel extends Model // 模型首字母大写并以Model为后缀,继承框架底层模型基类
{
public $table = 'user'; // 数据表
public $db = 'default'; // 数据库配置
}
namespace app\controllers;
class HomeController extends \newx\base\BaseController
{
public function actionIndex()
{
$get_all = $this->getRequest()->get(); // $_GET
$get_name = $this->getRequest()->get('name');
$post_all = $this->getRequest()->post(); // $_POST
$post_name = $this->getRequest()->post('name');
$header = $this->getRequest()->header(); // HEADER
}
}
namespace app\models;
class UserModel
{
public function test()
{
$request = \newx\base\Request::getInstance();
$get_all = $request->get();
$get_name = $request->get('name');
$post_all = $request->post();
$post_name = $request->post('name');
$header = $request->header();
}
}
namespace app\controllers;
class HomeController extends \newx\base\BaseController
{
public function actionIndex()
{
// JSON格式(默认)
$response = $this->getResponse()->success('success', []); // 成功响应
$response = $this->getResponse()->error('error', []); // 失败响应
// XML格式
$response = $this->getResponse(\newx\base\Response::CONTENT_TYPE_XML)->success('success', []);
return $response;
}
}
return [
// 初始化以default数据库配置执行,初始化前请先配置此项
'default' => [
'host' => '127.0.0.1',
'user' => 'root',
'password' => 'root',
'db' => 'chat',
'type' => 'mysqli'
]
];
$aes = new \newx\mcrypt\Aes(); // 默认CBC模式
$str = $aes->encrypt($str); // 加密(加密后默认base64编码,可用第二个参数更改)
$str = $aes->decrypt($str); // 解密
return [
'app' => [
// 服务器配置
'tcp' => [
'host' => '0.0.0.0',
'port' => 9501
],
'web-socket' => [
'host' => '0.0.0.0',
'port' => 9502
],
'http' => [
'host' => '0.0.0.0',
'port' => 9503
],
],
'database' => [
// 数据库配置,非必须配置项
'default' => [
'host' => '127.0.0.1',
'user' => 'user',
'password' => 'password',
'db' => 'db',
'type' => 'mysqli'
],
]
];