php - 在 Silex 中安装多个 Controller

标签 php silex

我正在尝试拥有多个具有多个安装点的 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/

相关文章:

php - 用mysql将多行插入数据库

php - Silex Payum 错误 : Request Capture{model: Identity} is not supported

php - 如何向匿名函数传递值?

php - 无需翻译的 Silex 表单验证

php - 如何使用 silex-annotation-provider 指定 Silex 路由名称?

javascript - 在 JS 变量中传递 '+' 符号

php - 计算日期时正确使用 PHP 和 MySQLi 查询

php - 在查询中使用 "WHERE"时 PDO 失败

php - 在 wp_insert_post_data 上的过滤器中获取自定义字段值

php - 如何使用php显示上传的csv文件数据