此包已被废弃,不再维护。未建议替代包。

基于Symfony组件和Eloquent ORM的小型MVC框架

维护者

详细信息

github.com/kletellier/MVC

源代码

问题

安装: 104

依赖: 0

建议者: 0

安全: 0

星级: 6

关注者: 2

分支: 1

开放问题: 0

语言:HTML

类型:项目

6.0.2 2018-04-20 13:20 UTC

README

使用Symfony组件、Eloquent ORM、FPDF、Redis和PHPExcel包装器的小型PHP MVC框架。

组件

  • Symfony
    • Http 基础
    • 路由
    • Yaml
    • 依赖注入
    • 文件系统
    • 查找器
    • 计时器
    • 配置
    • 控制台
  • Illuminate
    • Eloquent
  • Blade
  • PHPMailer
  • PSR 日志
  • Predis

目标

目标是创建一个小的框架来理解其内部工作原理,并在小型项目中使用。

安装

git clone https://github.com/kletellier/MVC.git /var/www
  1. 授予缓存、上传和tmp文件夹的写入权限

  2. 运行composer update --prefer-dist下载所有必需的包并创建自动加载脚本

  3. 在您的Web服务器上创建一个新的网站,指向public文件夹

  4. 更改网站配置文件

打开config/parameters.yml

config:
      debug: true
      webpath: ''
      template:
          engine: blade # section in template config below
          cache: false
          alwaysreload: true
      locale: en
database:
    default:
      server: 127.0.0.1
      port: 3306
      user: uid
      password: pwd
      database: dbname
templates: # classes must implement \GL\Core\Templating\TemplateServiceInterface
    twig:  
      class: \GL\Core\Twig\TwigService # need to install twig components
    blade:
      class: \GL\Core\Blade\Blade5Service
mail:
    server:  smtp.acme.com
    port: 587
    user: user
    password: pwd
    secure: 1
    encryption: "tls"
redis:
    default:
        server: 127.0.0.1
        port: 6379
        enable: 0
security:
    security:
         classes : GL\Core\Security\AuthenticationService
         roles_table : roles
         users_table : users
         usersroles_table : usersroles
    cookie:
         token : typeyoursecuritytokenhere
         name : REMEMBERME
         duration : 3600
    session:
         name: "kletellier"

debug参数显示错误描述。在生产网站上永远不要使用true。

webpath参数是您网站的根URL。

模板部分设置用于渲染模板的引擎(默认为blade)。

locale参数用于翻译对象(翻译文件存储在lang文件夹中)。

在模板部分,您可以添加自己的模板引擎实现,类必须实现\GL\Core\Templating\TemplateServiceInterface接口。

如果想要使用twig,只需在composer.json中添加twig,并在config/template/engine部分将类型从blade更改为twig。

邮件部分提供发送邮件的所有参数。您可以通过容器中的mailer对象发送邮件。(请参阅GL\Core\Tools\Mailer类)。

redis部分提供使用redis缓存系统的所有参数。

安全部分

- specify a security class who implement GL\Core\Security\AuthenticationServiceInterface , by default GL\Core\Security\AuthenticationService. 
- You must create table by using console security:create
- Select a cookie session name, duration 
  1. 路由文件配置

打开config/routes.yml

 pdf:
    pattern:  /pdf
    controller: default
    action: pdf  
xls:
    pattern:  /xls
    controller: default
    action: xls    
testdb:
    pattern:  /testdb
    controller: default
    action: testdb
root:
    pattern: /{name}
    controller: default
    action: hello
    defaults : { name : "World" }

每个路由必须有一个键名(部分名),并且必须包含模式、控制器和操作。您可以通过在模式部分添加变量来添加可选值,并且您还必须添加默认值部分,并为每个可选参数定义默认值。位置非常重要,因为用于确定选择的路由,解析器选择第一个匹配的URL模式。

工作原理

路由

我们在浏览器中输入https:///xls,路由组件检测您正在使用xls路由,控制器是default,要执行的操作是xls

如果没有匹配的路由,将执行ErrorController的404操作。

控制器

所有控制器都是继承自 \GL\Core\Controller\Controller 的类。你必须将此文件以规范化名称存储为 ControllernameController.php,在 /app/Application/Controllers 文件夹中。

在我们的示例中是 DefaultController.php。

之后,控制器解析组件将尝试实例化此控制器并尝试执行操作,在我们的例子中是 xls 函数。

控制器必须通过抽象控制器类提供的渲染方法返回 Symfony/Component/HttpFoundation/Response

使用

return $this->render(«Hello») ;

返回状态码为 200 的 Http 响应,包含 Hello 文本。

控制器嵌入许多渲染函数,如 renderJSON 用于将所有对象渲染为 JSON 字符串。

您也可以通过使用来重定向到其他操作/控制器

$this->redirect(«routename»,array(« param »=> value)) ; // routename is a route name defined in routes.yml

模板

并提供用于使用 Blade 模板引擎的渲染函数。

return $this->render('index',array(« params »=> « value »)) ;

渲染函数将数组中提供的所有参数提交给模板文件 index.blade.php。

所有模板都存储在 app/Application/Views

您可以在每个控制器中添加一些子文件夹,例如为默认控制器添加 Default 文件夹。

默认情况下,模板引擎将取控制器文件夹中的模板文件(在我们的例子中是 app/Application/Views/Default),如果找不到,它将尝试在 Views 根文件夹(app/Application/Views)中找到它们。

渲染函数返回一个 Symfony/Component/HttpFoundation/Response,您可以指定 Http 状态码(默认为 200),并通过重载方法添加头部作为键值数组。

如果您只想获取 Html,可以使用

return $this->renderHtmlTemplate('index',array(« params »=> « value »)) ;

它只返回原始 Html。

有关 Blade 的所有文档都在这里 [Blade] (https://laravel.net.cn/docs/5.1/blade)。

我添加了一个 url 函数,用于从相对 URL 获取绝对 URL,基于 config/parameters.yml 中的 webpath 参数

{{ Utils::url('/xls') }}

给出 https:///xls

您可以使用此命令清除缓存:php console cache:clear

您可以通过在模板中添加 @use(\Application\Classes\MyClass) 并调用 {{ MyClass:MyMethod($data) }} 来在 Blade 中添加自己的方法。

如果您不想使用 Blade,可以在视图文件夹中放置 PHP 文件并使用

return $this->renderPHP('index.php',array(« params »=> « value »)) ;

它就像一个包含文件。

依赖注入

每个控制器实例都有自己的 DI 容器,您可以使用 get 函数从该容器中检索每个服务

php $this->get('xls') // 将返回准备好工作的 PhpExcel 对象

每个容器中都有许多服务

  • mailer : \GL\Core\Tools\Mailer 实例,PHPMailer 的包装器。
  • request : Symfony\Component\HttpFoundation\Request 实例。
  • request_helper : GL\Core\Helpers\RequestHelper 实例。
  • template : GL\Core\Templating\TemplateProvider 实例。
  • routes : Symfony\Component\Routing\RouteCollection 所有在 config/routes.yml 中定义的路由。
  • pdf : GL\Core\Tools\PDF 实例,TCPDF 的包装器。
  • excel : GL\Core\Tools\Excel 实例,PhpExcel 的包装器。
  • session : Symfony\Component\HttpFoundation\Session\Session 实例。
  • crsf : GL\Core\Security\FormCrsf 实例。
  • translator : GL\Core\Config\Translator 实例。
  • security : GL\Core\Security\SecurityService 实例。
  • debug : GL\Core\Debug\KLDebugBar 实例(调试栏)。
  • pdo : 已启动数据库的 PDO 实例。
  • config : GL\Core\Config\Config 实例,用于读取 yml 配置文件。
  • db : GL\Core\Helpers\DbHelper 实例,用于数据库交互。
  • redis : GL\Core\Tools\Redis 实例,Predis 的包装器。

您可以通过在 config/services.yml 中添加引用来在 DI 容器中添加自己的服务。

这是Symfony yml格式,更多信息请见这里

https://symfony.com.cn/doc/current/components/dependency_injection/introduction.html

Eloquent ORM

您可以在app/Application/Models文件夹中创建模型。

例如,您数据库中有一个测试表。

您创建了一个名为Test的PHP类,它继承自Illuminate\Database\Eloquent\Model

将其存储在app/Application/Models/Test.php

namespace Application\Models;

use Illuminate\Database\Eloquent\Model;

class Test extends Model {
    protected $table = 'test';
    public $timestamps = false;
}

在您的控制器中,您可以访问该表中的所有数据,

// retrieve all entries from test table 
$tests = Test::all() ;

// retrive only few entries
$test2 = Test::where('column','=','value') ;

有关Eloquent ORM的更多信息

[Eloquent ORM] (https://laravel.net.cn/docs/eloquent)

文档

关于Symfony组件

[Symfony] (https://symfony.com.cn/fr/components)

关于Eloquent ORM

[Eloquent ORM] (https://laravel.net.cn/docs/eloquent)

关于Blade

[Blade] (https://laravel.net.cn/docs/5.1/blade)

关于PHPMailer

[PHPMailer] (https://github.com/PHPMailer/PHPMailer)