I'm struggling to create a working MVC structure for a project.
What I'm using:
- PHP-DI for DI Container
- skipperbent/simple-php-router for routing
- Symfony's HttpFoundation
Here is my code.
container.php
$containerBuilder = new \DI\ContainerBuilder();
$containerBuilder->useAutowiring(true);
$containerBuilder->useAnnotations(true);
$containerBuilder->addDefinitions(__DIR__ . '/container-config.php');
$container = $containerBuilder->build();
container-config.php
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Psr\Container\ContainerInterface;
return [
'request' => function() {
return Symfony\Component\HttpFoundation\Request::createFromGlobals();
},
'session' => function() {
return new Symfony\Component\HttpFoundation \Session\Session();
}
Environment::class => function () {
$loader = new FilesystemLoader(__DIR__ . '/../templates');
$twig = new Environment($loader, [
'cache' => __DIR__ . '/../var/cache/templates',
]);
return $twig;
},
];
router.php
use Pecee\SimpleRouter\SimpleRouter as R;
use Pecee\Http\Middleware\BaseCsrfVerifier;
use App\App;
R::csrfVerifier(new BaseCsrfVerifier());
R::setDefaultNamespace('\App\Controller');
R::enableDependencyInjection($container);
R::get('/', 'ProductController@index', ['as' => 'products']);
R::start();
Here is my base controller
namespace App;
use Symfony\Component\HttpFoundation\Request;
use Twig\Environment;
class BaseController
{
protected $request;
protected $twig;
public function __construct(Request $request, Environment $twig)
{
$this->request = $request;
$this->twig = $twig;
}
}
Finally, my product controller
namespace App\Controller;
use App\BaseController;
class ProductController extends BaseController
{
public function index()
{
dump($this->request); // returns the request object
dump($this->request->query->all()); // return empty array
}
}
The first problem is that the request object I set on the container, does not wok inside my controller.
Example url example.com?foo=bar
dump($this->request)
This line returns a Symfony\Component\HttpFoundation\Request
as it should, but it seems to be a new one, because I can't get the query params with $this->request->query->all()
, it returns empty.
If I create Symfony\Component\HttpFoundation\Request::createFromGlobals();
as a global variable, it works as expected, and dumping $this->request->query->all()
returns the expected array.
So my question is, how do I best couple all these components together to be able to have a working structure?
Thank you!