当我尝试获取当前目录时:
$this->container->getParameter('kernel.root_dir').'/../web/
我有这个错误:Fatal error: Using $this when not in object context in C:'XXX on line 124
法典:
class AdminController {
/**
* Add event controller.
*
* @param Request $request Incoming request
* @param Application $app Silex application
*/
public function addEventAction(Request $request, Application $app) {
$event = new Event();
$types= $app['dao.type']->findAllSelectList();
$eventForm = $app['form.factory']->create(new EventType($types), $event);
$eventForm->handleRequest($request);
if ($eventForm->isSubmitted() && $eventForm->isValid()) {
var_dump($event->getCoverImageLink());
$file = $event->getCoverImageLink();
$fileName = md5(uniqid()).'.'.$file->guessExtension();
var_dump($fileName);
//$path = $this->container->getParameter('kernel.root_dir').'/../web';//$this->get('kernel')->getRootDir() . '/../web';
var_dump($this);
$app['dao.event']->save($event);
$app['session']->getFlashBag()->add('success', 'The event was successfully created.');
}
return $app['twig']->render('event_form.html.twig', array(
'title' => 'New event',
'eventForm' => $eventForm->createView()));
}
请问如何解决此错误?使用什么正确的功能?
看来您使用的是Silex
,而不是Symfony 2
。作为一个非常简约的框架,silex并没有给你Symfony所做的所有配置和依赖注入的好处。
能够检索应用程序根目录的最简单方法是在 bootstrap.php
中自行定义它。只需在顶部添加这样的东西:
define('APP_ROOT', __DIR__ . '/../');
现在,您只需在控制器中使用常量:
public function addEventAction(Request $request, Application $app) {
...
$path = APP_ROOT . '/../web';
...
}