在Silex (Symfony2)中创建子请求后变量不再存在



我有这个问题,我试图在Silex中创建子请求,并基本上将我的参数转发给另一个控制器。附录A在下面被打破了(在尝试重构之后),而附录B,原始版本,工作:

Exhibit A ($this->app在创建请求后丢失):

class EntriesController {
    private $app;
    private $req;
    public function __construct($app, $req) {
        $this->app = $app;
        $this->req = $req;      
    }
    public function updateAction() {
        //...
            //$url defined here (eyesore-ingly long, so not shown)
            $subRequest = Request::create($url, 'GET', $params,  $this->req->cookies->all(), array(), $this->req->server->all());
            //$this->app **no longer** exists here
            return $this->app->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
        }
    }

EntriesController实例创建如下:

class AppControllerProvider implements ControllerProviderInterface {
    public function connect(Application $app) {     
        $controllers = $app['controllers_factory'];
        //...
        $controllers->patch('/edit', function (Request $req) use ($app) {
            $entriesCtrl = new EntriesController($app, $req);
            return $entriesCtrl->updateAction();                
        });
        //...
}

展品B(工作正常):

 class AppControllerProvider implements ControllerProviderInterface {
    public function connect(Application $app) {     
        $controllers = $app['controllers_factory'];
        $controllers->patch('/edit', function (Request $req) use ($app) {
            //...
            //$url defined here
            $subRequest = Request::create($url, 'GET', $params,  $this->req->cookies->all(), array(), $this->req->server->all());    
            return $this->app->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
        }
        //...       
    });

我基本上只是将Exhibit B'PATCH' /edit方法体中的逻辑重新组织到控制器类中,并且我将Silex应用程序实例$app传递给控制器类的新实例。

据我所知,例证A和例证B之间的唯一区别是您在路径方法回调中实例化了控制器对象。也许这个控制器的设置或名称空间问题有问题?在黑暗中拍摄。

我可以确认,在我的silex应用程序中,以下代码产生空$app容器:

GlobalControllerProvider.php

<?php
namespace DevPubProviderController;
use SilexApplication;
use SilexControllerProviderInterface;
use SymfonyComponentHttpFoundationRequest;
class GlobalControllerProvider implements ControllerProviderInterface
{
    public function connect(Application $app)
    {
        $controllers = $app['controllers_factory'];
        $controllers
            ->get('/', 'DevPubControllerGlobalController::indexAction')
            ->bind('homepage')
        ;
        $controllers
            ->patch('/edit', function (Request $req) use ($app) {
                $entriesCtrl = new DevPubControllerGlobalController();
                return $entriesCtrl->updateAction($app, $req);
        });
        return $controllers;
    }
}

GlobalController.php

<?php
namespace DevPubController;
use SilexApplication;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentHttpKernelHttpKernelInterface;
class GlobalController
{
    public function indexAction(Application $app, Request $request)
    {
        return new Response($app['twig']->render('index.html.twig'));
    }
    public function updateAction(Application $app, Request $request)
    {
        $url = 'http://silex.local/index_dev.php/'; 
        $params = array();
        $subRequest = Request::create($url, 'GET', $params,  $request->cookies->all(), array(), $request->server->all());
        // outputs: 'SilexApplication'
        error_log(print_r(get_class($app),1).' '.__FILE__.' '.__LINE__,0); 
        // outputs: 1
        error_log(print_r(is_object($app),1).' '.__FILE__.' '.__LINE__,0);  
        return $app->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
    }
}

index.main.js

$(function(){
    console.log('index.main.js');
    $.ajax({
        url: "http://silex.local/index_dev.php/edit",
        method: "PATCH"
    }).done(function( data ) {
        console.log(data);
    }).fail(function( data ) {
        console.log(data);
    });
});

最新更新