rudraks/boot

Rudrax 支持库

0.0.21 2020-06-07 15:19 UTC

README

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

设置

先决条件

设置新的 RudraX 项目

如果你正在启动一个新的项目,只需按照以下步骤操作

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

或者你可以手动设置 RudraX 项目或在现有项目中安装 RudraX

在现有项目中安装

$ composer install rudrax/boot --save

文件夹结构

如果你使用了 create-project 命令,则此目录结构将自动为你创建。否则,请手动创建它,并确保构建文件夹具有正确的权限 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