我正在尝试拥有多个具有多个安装点的 Controller 。代码如下
GearmanController.php
use Silex\Application;
class GearmanController implements \Silex\ControllerProviderInterface
{
public function connect(Application $app) {
$controllers = $app['controllers_factory'];
$controllers->get('/info', function() use ($app){
error_log("gearman prcoesses has ben called");
return new \Symfony\Component\HttpFoundation\Response('gearman prcoesses has ben called');
});
return $controllers;
}
public function boot(Application $app)
{
// TODO: Implement boot() method.
}
}
SupervisorController
<?php
use Silex\ControllerProviderInterface;
use Silex\Application;
class SupervisorController implements ControllerProviderInterface
{
public function connect(Application $app) {
$controllers = $app['controllers_factory'];
$controllers->get('/processes', function() use ($app){
error_log("supervisor prcoesses has ben called");
return new \Symfony\Component\HttpFoundation\Response('gearman prcoesses has ben called');
});
return $controllers;
}
public function boot(Application $app)
{
// TODO: Implement boot() method.
}
}
bootstrap.php
<?php
require_once __DIR__ . "/../vendor/autoload.php";
?>
routes.php
<?php
$app->mount('/supervisor', new SupervisorController());
$app->mount('/gearman', new GearmanController());
?>
和index.php
<?php
$app = require_once __DIR__ . '/../app/app.php';
$app->run();
?>
app.php
<?php
require_once __DIR__ . '/bootstrap.php';
$app = new Silex\Application();
require_once __DIR__ . '/../src/routes.php';
return $app;
?>
看起来很简单,但是当我用
点击网址时/gearman/info
/supervisor/processes
我收到 404 Not Found
并且 php 错误日志中没有打印任何内容。
最佳答案
您的 Controller 提供程序有错误。将路由添加到 Controller 集合,而不是应用程序。并在操作中返回响应。
public function connect(Application $app)
{
$controllers = $app['controllers_factory'];
$controllers->get('/info', function() use ($app){
error_log("gearman prcoesses has ben called");
return new \Symfony\Component\HttpFoundation\Response('gearman prcoesses has ben called');
});
return $controllers;
}
还将所有 404 请求转发到index.php。为此进行 .htaccess
配置:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</IfModule>
关于php - 在 Silex 中安装多个 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35521539/