在 Bolt 扩展中实现控制器和 ControllerProvider
Implementing Controllers and ControllerProviders in a Bolt Extension
我目前正在尝试在 Bolt (v2.0.6) 扩展中实现一个控制器,它可以处理页面的 post 请求,我有以下 类.
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
正在被实例化,因为我可以在 connect()
函数期间 exit()
并将其输出到浏览器。但是,我似乎无法触发 ContactController
中的 store()
函数。我也尝试了 $contollers->get()
并得到了相同的结果。
我似乎做错了什么,或者遗漏了一些完整的东西。任何帮助将不胜感激。
这就是我的处理方式。
Extension.php
namespace MyPlugin;
class Extension extends \Bolt\BaseExtension {
function initialize()
{
$this->app->mount('/contact', new MyPlugin\Controllers\ContactController());
}
}
Controllers\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!');
}
}
我目前正在尝试在 Bolt (v2.0.6) 扩展中实现一个控制器,它可以处理页面的 post 请求,我有以下 类.
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
正在被实例化,因为我可以在 connect()
函数期间 exit()
并将其输出到浏览器。但是,我似乎无法触发 ContactController
中的 store()
函数。我也尝试了 $contollers->get()
并得到了相同的结果。
我似乎做错了什么,或者遗漏了一些完整的东西。任何帮助将不胜感激。
这就是我的处理方式。
Extension.php
namespace MyPlugin;
class Extension extends \Bolt\BaseExtension {
function initialize()
{
$this->app->mount('/contact', new MyPlugin\Controllers\ContactController());
}
}
Controllers\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!');
}
}