在 Silex 中安装多个控制器
Mounting multiple controllers in 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;
?>
看起来很简单,但是当我用
点击url
/gearman/info
/supervisor/processes
我收到 404 Not Found
并且 php 错误日志中没有打印任何内容。
您的控制器提供程序有误。将路由添加到控制器集合,而不是应用程序。并且 return 响应行动。
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>
我正在尝试让多个控制器具有多个安装点。我们的代码如下
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;
?>
看起来很简单,但是当我用
点击url/gearman/info
/supervisor/processes
我收到 404 Not Found
并且 php 错误日志中没有打印任何内容。
您的控制器提供程序有误。将路由添加到控制器集合,而不是应用程序。并且 return 响应行动。
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>