我目前正在尝试在Bolt(v2.0.6)扩展中实现一个可以处理页面发布请求的控制器,我有以下类。
Extension.php
namespace MyPlugin;
class Extension extends \Bolt\BaseExtension {
function initialize()
{
$this->app->mount('/contact', new MyPlugin\Controllers\ContactControllerProvider);
}
ContactController.php
namespace MyPlugin\Controllers;
class ContactController
{
public function store()
{
// handle the POST request
exit('store');
}
ContactControllerProvider.php
namespace MyPlugin\Controllers;
use Silex\Application;
use Silex\ControllerProviderInterface;
class ContactControllerProvider implements ControllerProviderInterface
{
public function connect(Application $app)
{
// creates a new controller based on the default route
$controllers = $app['controllers_factory'];
// attempting to add POST hook
$controllers->post("/", "MyPlugin\Controllers\ContactController::store");
return $controllers;
}
}
我知道ContactControllerProvider
正在实例化,因为我可以在exit()
函数中connect()
并输出到浏览器。但是,我似乎无法启动store()
中的ContactController
功能。我也试过了$contollers->get()
并得到了相同的结果。
我似乎做错了什么,或者错过了一些完整的东西。任何帮助将不胜感激。
答案 0 :(得分:1)
这是我接近它的方式。
<强> Extension.php 强>
namespace MyPlugin;
class Extension extends \Bolt\BaseExtension {
function initialize()
{
$this->app->mount('/contact', new MyPlugin\Controllers\ContactController());
}
}
<强>控制器\ ContactController.php 强>
namespace MyPlugin\Controllers;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Silex\Application;
use Silex\ControllerProviderInterface;
class ContactController implements ControllerProviderInterface
{
public function connect(Application $app)
{
/** @var $ctr \Silex\ControllerCollection */
$ctr = $app['controllers_factory'];
// This matches both GET requests.
$ctr->match('', [$this, 'show'])
->method('GET');
// This matches POST requests.
$ctr->match('', [$this, 'store'])
->method('POST');
return $ctr;
}
public function show(Application $app, Request $request)
{
return new Response('Hello, World!');
}
public function store(Application $app, Request $request)
{
return new Response('It was good to meet you!');
}
}