在Silex中安装多个控制器

时间:2016-02-20 09:46:40

标签: php silex

我正在尝试使用多个安装点的控制器。我们的代码如下

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错误日志中没有打印任何内容。

1 个答案:

答案 0 :(得分:1)

您在控制器提供程序中有错误。添加路由到控制器集合,而不是应用程序并在行动中回复。

http://yourdomain.com/index.html

还将所有404请求转发给index.php。为此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; } 配置:

.htaccess