我正在使用Slim框架创建API。目前我使用单个文件来创建路径并向其传递闭包:
$app->get('/', function($req, $resp){
//Code...
})
但我意识到我的文件发展迅速。我想要做的是使用控制器,所以我将有一个控制器类,只需将实例/静态方法传递给路由,如下所示
class HomeController
{
public static function index($req, $resp){}
}
然后将函数传递给路径
$app->get('/', HomeController::index);
我尝试了这个,但它不起作用,我想知道是否有办法可以用它来管理我的文件。
答案 0 :(得分:9)
将控制器变为仿函数:
echo $array['rating'];
然后像这样路线:
class HomeController
{
public function __invoke($req, $resp) {}
}
供参考,见
答案 1 :(得分:2)
PHP 5.6 超薄2.6.2
require 'vendor/autoload.php';
class HelloController {
public static function index() {
global $app;
echo "<pre>";
var_dump($app->request);
echo "</pre>";
}
}
$app = new \Slim\Slim();
$app->get('/', 'HelloController::index');
$app->run();
更新: PHP 5.6 Slim 3.0.0
require 'vendor/autoload.php';
class HelloController {
public static function hello(\Slim\Http\Request $req, \Slim\Http\Response $response, $args) {
echo "<pre>";
var_dump($args);
echo "</pre>";
}
}
$app = new \Slim\App();
$app->get('/hello/{name}', 'HelloController::hello');
$app->run();
Slim 3.0中基于类路由的问题是对$this
/ $app
的访问。我认为您需要使用global $app
来访问它。
在我的宠物项目中,我使用require_once
的路线组。像
$app->group('/dashboard', function () {
$this->group('/auctions', function () use ($app){
require_once('routes/dashboard/auctions.php');
});
$this->group('/rss', function () {
require_once('routes/dashboard/rss.php');
});
$this->group('/settings', function () {
require_once('routes/dashboard/settings.php');
});
});
看起来并不像纯粹的课程那样漂亮,但是如果没有额外的编码就可以获得所有功能。
答案 2 :(得分:2)
顺畅&amp;将控制器用作对象的简短方法(不是静态方式)
index.php中的
<java version="1.8.0_172+ 1.8.0_152 1.8.0_151 1.8.0_144 1.8.0_141 1.8.0_131 1.8.0_121 1.8.0_112 1.8.0_111 1.8.0_102 1.8.0_101 1.8.0_92 1.8.0_91 1.8.0_74 1.8.0_73 1.8.0_72 1.8.0_71 1.8.0_66 1.8.0_65 1.8.0_60 1.8.0_51 1.8.0_45 1.8.0_40 1.8.0_31 1.8.0_25 1.8.0_20 1.8.0_11 1.8.0_05"/>
在Controller / MyClass中:
namespace MyApp;
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require __DIR__ . '/../vendor/autoload.php';
$app->get('/myroute', [new Controller\MyClass, 'get']); // <=== that is pretty short and neat
$app->post('/myroute', [new Controller\MyClass, 'post']);
$app->map(['GET', 'POST'], '/myotherrout', [new Controller\MyOtherClass, 'run']);
通过使用PSR自动加载来解析Controller \ MyClass
在Controller / MyOtherClass中:
namespace MyApp\Controller;
class MyClass{
public function __construct(){
//some code
}
public function get(\Slim\Http\Request $request, \Slim\Http\Response $response, $args = []) {
//some super foobar code
}
public function post(\Slim\Http\Request $request, \Slim\Http\Response $response, $args = []) {
//some other code
}
答案 3 :(得分:2)
以下是一个例子:
<?php
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
/**
* @property LoggerInterface $logger;
*/
class Controller
{
/**
* @var LoggerInterface
*/
protected $logger
/**
* @param LoggerInterface $logger
*/
public function __construct($logger)
{
$this->logger = $logger;
}
public function action(ServerRequestInterface $request, ResponseInterface $response, $args=[])
{
$this->logger->info((string)$request->getUri());
/* some actions */
return $response;
}
}
<?php
use Slim\App;
use Slim\Container;
use Psr\Container\ContainerInterface;
$autoloader = require(__DIR__.'/vendor/autoload.php');
$container = new Container();
$container['logger'] = function($container) {
return new Logger();
}
$container['some.controller'] = function ($container) {
/**
* @var ContainerInterface $container
*/
$logger = $container->get('logger');
return new Controller($logger);
};
$app = new App($container);
$app->get('/some/route', 'some.controller:action');
$app->run();
文档click me
中描述了此方法答案 4 :(得分:1)
Nikic的快速路由是一个非常小的路由器,因此删除了更大框架的一些细节。这是一个基本的解决方案:
routes.php文件
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app->get('/', function($req, $resp, $args) use ($app){return FooBar::asdf($app, $req, $resp);});
控制器 使用\ Psr \ Http \ Message \ ServerRequestInterface作为Request; 使用\ Psr \ Http \ Message \ ResponseInterface作为响应;
class FooBar{
static public function asdf(Slim\App $app, Request $req, Response $resp, $args = [])
{
return $resp->withJson(['asf']);
}
}