I started to read symfony book and try to implement on my own a really basic mvc framework for learning purpose. I use some components of symfony. By now i have an index.php file like this
<?php require_once 'vendor/autoload.php'; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; $request = Request::createFromGlobals(); $uri = $request->getPathInfo(); $frontController = new FrontController($uri); Then i have a frontController.php file that have a basic routing
<?php class FrontController { private $uri; public function __construct($uri) { $this->uri = $uri; $this->setRoute(); } private function setRoute() { $request = explode("/", trim($_SERVER['REQUEST_URI'])); $controller = !empty($request[1]) ? ucfirst($request[1]) . "Controller" : "IndexController"; $action = !empty($request[2]) ? $request[2] . "Action" : "indexAction"; $parameters = !empty($request[3]) ? intval($request[3]) : null; $response = call_user_func(array($controller, $action), $parameters); $this->sendResponse($response); } private function sendResponse($response) { $response->send(); } } Then i have a series of controller (i.e. ordersController.php)
<?php use Symfony\Component\HttpFoundation\Response; class OrdersController { public function ordersAction() { //take data from model $order = new OrdersModel; $orders = $order->get_all_orders(); $html = self::render_template('templates/list.php', array('orders' => $orders)); return new Response($html); } public function render_template($path, array $args = null) { if(!empty($args)) extract($args); ob_start(); require $path; $html = ob_get_clean(); return $html; } } Now, although i know that what i have done can be improved , i would like to inject the right model when i instantiate a controller class in the FrontController class without instantiate the model instance in the method where i use that. How can i achieve this? And if you have some suggests to give me, please do that because i want to improve my skills.