rudrax/boot

Rudrax 的支持库

0.0.21 2020-06-07 15:19 UTC

README

PHP 中的简单轻量级 MVC 框架,它提供所有稳定的功能,如路由(原生)、缓存(phpFastCache)、数据库(Redbeans & PDO)、模板(Smarty)、认证(基本、SSO & 自定义)、Session/Roles 等。

设置

先决条件

设置新的 RudraX 项目

如果您是开始一个新的项目,只需按照以下步骤操作

  • 转到您想创建项目的目录,例如 "/Users/Lucas/Projects/",然后运行此命令
$ composer create-project rudrax/project my_project

或者,您可以手动设置 RudraX 项目或将 RudraX 安装到现有项目中

在现有项目中安装

$ composer install rudrax/boot --save

文件夹结构

如果您已使用 create-project 命令,则此目录结构将自动为您创建。否则,请手动创建它,并确保 build 文件夹有正确的权限 0777

-app
  L controller  // Controller/URL Mapping for Project, name of file and class should match
  L model       //Models being Used in Porject
  L view        //Smart Templates
-config
  L project.properties //Project Properties
-build          // Build/Temporary Files created by Framwork, need write permissions
-src            // Folder for static files like javascript,html,css 
-lib            // composer library folder
-index.php
.htaccess
-composer.json  // set config->vendor-dir = lib

索引文件 [index.php]

如果您已使用 create-project,则此文件将自动创建。否则,创建它并复制文件中的代码,同时将 .htaccess 复制到您的项目中。

ini_set('display_errors', 'On');
ini_set('error_reporting', E_ALL);
error_reporting(E_ALL);
error_reporting(E_ALL & ~E_DEPRECATED);


require("./lib/autoload.php");
require_once("./lib/rudrax/boot/RudraX.php");

RudraX::invoke(array(
    'RX_MODE_MAGIC' => TRUE,
    'RX_MODE_DEBUG' => FALSE,
    'PROJECT_ROOT_DIR' => "./"
));

文档

现在我们已经完成了设置,让我们开始了解如何在项目中编写代码。

示例控制器 [app/controller/MyController.php]

namespace app\controller {

    class MyController extends AbstractController
    {
        /**
         * @RequestMapping(url="login",method="GET",type="template")
         * @RequestParams(true)
         */
        public function login($model,$username,$password)
        {
            if($username == "user1" && $password == "xDddfdfd"){
              $this->user->setValid(TRUE);
              $this->user->role = "USER";
              $model->assign("username", $username);
              return "welcome"; // 'welcome' is path of smarty tempalte file in view folder
            } else {
              $this->user->setValid(FALSE);
               $model->assign("error", "Wrong credentials");
              return "login"; // 'login' is path of smarty tempalte file in view folder
            }
        }
        
        /**
         * @RequestMapping(url="myprofile",method="GET",type="template")
         * @RequestParams(true)
         */
        public function myprofile($model)
        {
            if($this->user->isValid()){
              $model->assign("username", $this->user->uname);
              return "user/myprofile"; // 'user/myprofile' is path of smarty tempalte file in view folder
            } else {
              $this->user->setValid(FALSE);
               $model->assign("error", "You need to login to view this page");
              return "login"; // 'login' is path of smarty tempalte file in view folder
            }
        }
        
        /**
         * @RequestMapping(url="info/school/{category}",method="GET",type="json")
         * @RequestParams(true)
         */
        public function schoolinfo($category)
        {
            if($this->user->isValid()){
              return array( "success" => true, "id" => 23,"name"=>"DAV Public School");
            } else {
              return array("success" => false,"error"=> "You need to login to view this info");
            }
        }

    }
}

控制器注释选项

所有都是方法级别注释

  • @RequestMapping - URL 信息 - url - 匹配的 URL 模式 - method - 请求方法 [GET|POST|PUT|DELETE] - 仅当提及时使用 - type - 响应类型 [template|json|data] - 数据 - auth - 如果 URL 访问需要基本认证 [TRUE|FALSE] - FALSE - cache - 如果响应可由服务器缓存 [TRUE|FALSE] - FALSE - guestcache - 仅在访客用户(用户无效)的情况下可缓存 [TRUE|FALSE] - FALSE
  • @RequestParams - 如果要在控制器中获取并使用查询参数。 [TRUE|FALSE] - FALSE
  • @Role - [用户定义的值] - 仅当提及时使用,具有匹配 $user-{role} 的用户将有权访问 API。

模型注释选项

类级别注释

  • @Model - [sessionUser] -
    • sessionUser - 如果使用,则该模型将用作默认会话用户,类必须扩展 app\model\AbstractUser

调整

要构建项目/清除缓存/重建注释 - 从浏览器中访问此 URL

    https:///?RX_MODE_BUILD=TRUE&RX_MODE_DEBUG=TRUE