PHP code example of beansir / newx-framework
1. Go to this page and download the library: Download beansir/newx-framework 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-framework 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;
}
}