jaxon-php / jaxon-slim
Slim 框架的 Jaxon 库集成
v4.0.0
2022-07-30 14:30 UTC
Requires
- jaxon-php/jaxon-core: ^4.0
- slim/slim: 4.*
README
此包将 Jaxon 库 集成到 Slim 框架中。
安装
该包的版本 4 需要 Slim 版本 4。
使用 Composer
安装此包。
composer require jaxon-php/jaxon-slim ^4.0
或者
{ "require": { "jaxon-php/jaxon-slim": "^4.0", } }
运行 composer install
。
路由和中间件
此包使用两个 Jaxon PSR 中间件,一个用于加载 Jaxon 配置,另一个用于处理 Jaxon 请求。Jaxon 配置中间件必须附加到启用 Jaxon 功能的所有路由上,而后者必须附加到处理 Jaxon 请求的路由上。
<?php use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Server\RequestHandlerInterface as RequestHandler; use Slim\Factory\AppFactory; use Slim\Psr7\Response; use function Jaxon\jaxon; require __DIR__ . '/../vendor/autoload.php'; $app = AppFactory::create(); // Jaxon middleware to load config // Set a container if you need to use its services in Jaxon classes. // Set a logger if you need to send messages to your logs in Jaxon classes. $jaxonConfigMiddleware = function(Request $request, RequestHandler $handler) { return jaxon()->psr() // Uncomment the following line to set a container // ->container($container) // Uncomment the following line to set a logger // ->logger($logger) ->config(__DIR__ . '/../config/jaxon.php')->process($request, $handler); }; // Process Jaxon ajax requests $app->group('/', function() use($app) { // Jaxon middleware to process ajax requests $jaxonAjaxMiddleware = function(Request $request, RequestHandler $handler) { return jaxon()->psr()->ajax()->process($request, $handler); }; $app->post('/jaxon', function($request, $response) { // Todo: return an error. Jaxon could not find a plugin to process the request. })->add($jaxonAjaxMiddleware); // Insert Jaxon codes in a page $app->get('/', function($request, $response) { // Display a page with Jaxon js and css codes. $jaxon = jaxon()−>app(); $css = $jaxon->css(); $js = $jaxon->js(); $script = $jaxon->script(); // Display the page ... }); })->add($jaxonConfigMiddleware);
设置视图渲染器
Slim 框架提供了两个用于视图渲染的组件,并且都可以与 Jaxon 视图渲染器 一起使用。
Twig-View 组件用于显示 Twig 视图。
use Jaxon\Slim\Helper; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Server\RequestHandlerInterface as RequestHandler; use Slim\Views\TwigMiddleware; // Add Twig-View Middleware $twig = Helper::twig(__DIR__ . '/../templates', ['cache' => false]); $app->add(TwigMiddleware::create($app, $twig)); $jaxonConfigMiddleware = function(Request $request, RequestHandler $handler) { return jaxon()->psr() ->view('twig', '.html.twig', function() use($request) { return Helper::twigView($request); }) ->config(__DIR__ . '/../jaxon/config.php') ->process($request, $handler); };
PHP-View 组件用于显示 PHP 视图。
$jaxonConfigMiddleware = function(Request $request, RequestHandler $handler) { return jaxon()->psr() ->view('php', '.php', function() { return Helper::phpView(__DIR__ . '/../templates'); }) ->config(__DIR__ . '/../jaxon/config.php') ->process($request, $handler); };
使用方法
在 config/jaxon.php
配置文件中的设置分为两个部分。位于 lib
部分的选项是 Jaxon 核心库的选项,而位于 app
部分的选项是 Slim 应用程序的选项。
可以在配置文件的 app
部分中定义以下选项。
默认情况下,views
数组为空。视图从框架默认位置渲染。在 directories
数组中有一个条目,其值如下。
使用方法
Jaxon 类
Jaxon 类可以继承自 \Jaxon\App\CallableClass
。默认情况下,它们位于 Slim 应用程序的 jaxon/ajax
目录中,关联的命名空间是 \Jaxon\Ajax
。
以下是一个 Jaxon 类的示例,定义在 ROOT/jaxon/Ajax/HelloWorld.php
文件中。
namespace Jaxon\Ajax; class HelloWorld extends \Jaxon\App\CallableClass { public function sayHello() { $this->response->assign('div2', 'innerHTML', 'Hello World!'); return $this->response; } }
贡献
- 问题跟踪器:github.com/jaxon-php/jaxon-slim/issues
- 源代码:github.com/jaxon-php/jaxon-slim
许可证
该包采用 BSD 许可证。