Struts的PHP实现

v0.9-alpha 2014-09-23 12:05 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:35:40 UTC


README

当前框架互操作性标准下的Struts的PHP实现。

Travis build status master Coverage Status SensioLabsInsight

  • 通过Silex(例如,获取依赖注入、调度器、安全、缓存、esi等,并支持构建其他服务)运行
  • 使用Symfony HTTPFoundation/HTTPKernel
  • 实现PSR的(命名约定、日志、自动加载)
  • 使用Twig或其他偏好依赖项

什么是Phruts?

Phruts基于Apache Struts,一个Java MVC框架。您的控制器通过XML文件配置,这还控制了应用程序流程和转发。有些部分做得很好,有些部分由其他框架做得更好。

安装

添加到您的composer中,然后执行composer update

"require": {
    "cammanderson/phruts": "~0.9"
}

添加到您的Silex应用程序中

// web/index.php
require_once __DIR__.'/../vendor/autoload.php';

// Use standard HttpFoundation
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;

// Create a silex application
$app = new Silex\Application();

// Add in Twig as the template handler
$app->register(new Silex\Provider\TwigServiceProvider(), array('twig.path' => __DIR__.'/../app/Resources/views'))
    ->register(new Silex\Provider\MonologServiceProvider(), array('monolog.logfile' => __DIR__.'/../app/logs/development.log'));

// Add in phruts to organise your controllers
$app->register(new Phruts\Provider\PhrutsServiceProvider(), array(
        // Register our modules and configs
        Phruts\Util\Globals::ACTION_KERNEL_CONFIG => array(
            'config' => '../app/config/web-config.xml', // Supports multiple modules/configurations
        )
    ));

// Add template handling for matching forwards to Twig templates
$app->get('{path}', function($path) use ($app) {
        return $app['twig']->render($path);
    })
    ->assert('path', '.+\.twig');

// Add routes to be matched by Phruts
$app->get('{path}', function (Request $request) use ($app) {
        return $app[\Phruts\Util\Globals::ACTION_KERNEL]->handle($request, HttpKernelInterface::SUB_REQUEST, false);
    })
    ->assert('path', '.*')
    ->value('path', '/'); // Set the welcome path

$app->run();

配置您的MVC配置

创建您的app/config/web-config.xml。此配置使用Phigester进行处理,该工具将读取XML配置并将其反射到应用程序配置中。然后配置被冻结并缓存。

<?xml version="1.0" encoding="UTF-8"?>
<phruts-config>
    <form-beans>
        <form-bean name="donateForm"
                   type="\MyProject\Forms\Donate" />
    </form-beans>

    <global-exceptions>
        <exception type="\Exception"
                   key="some.exception.key"
                   path="error.html.twig"/>
    </global-exceptions>

    <global-forwards>
        <forward name="welcome" path="/welcome.html.twig"/>
    </global-forwards>

    <action-mappings>
        <action path="/"
                type="\Phruts\Actions\ForwardAction"
                parameter="welcome"/>

        <action path="/donate"
                input="/"
                name="donateForm"
                type="\MyProject\DonateAction">
                <forward name="success" path="/thanks.html.twig"/>
                <forward name="failure" path="/donate-failed.html.twig"/>
        </action>
    </action-mappings>
</phruts-config>

额外事项

  • 创建您的app/cache文件夹(这是一个配置选项)
  • 创建一个twig模板app/Resources/views/welcome.html.twig

功能支持

  • 错误处理器
  • 全局转发
  • 基于Symfony Security定义的角色的请求处理器
  • 动作转发(内部动作链式调用)
  • 通配符路径匹配
  • 动作切换和多模块支持
  • 多模块配置合并(例如,多config.xml)
  • Struts 1.2 API

当前开发状态

  • 已完成:代码覆盖率35%+
  • 已完成:代码覆盖率75%+
  • 已完成:最终确定ActionKernel以在Silex\Application上安装
  • 进行中:确认框架功能的Struts 1.2代码覆盖率
  • 进行中:编写标准的Struts风格Twig扩展以访问表单+消息
  • 接下来:Silex的工作文档
  • 考虑Struts验证框架(我们从未使用过它,所以可能不会推进)
  • Struts 1.3框架功能(仅作为便利技术的使用)

我为什么要这样做?学习Web开发技术的最佳方法是研究Request->Response Web框架。我对旧的Struts应用程序有感情,我们有一些遗留的PHPMVC代码,我们希望轻松将其向前推进。这个项目使我们能够轻松迁移遗留代码并快速现代化,通过实现PSR,为代码库提供一个有未来的支持生命。