“Proper” separation/difference between index.php and front controller

前端 未结 3 987
终归单人心
终归单人心 2020-12-16 05:46

For a PHP MVC application, what is the difference of the job of the index.php file and front-controller? Is the front-controller in the index.php,

相关标签:
3条回答
  • 2020-12-16 05:51

    Index.php should initialize the application and call something that deciphers the route into controller and action, and runs them. Look at Yii, Symfony, CodeIgniter, CakePHP, see what they do. All slightly different but same principle.

    An example from Yii's index.php to make the point:

    <?php
    
    $yii=dirname(__FILE__).'/../../framework/yii.php';
    $config=dirname(__FILE__).'/protected/config/main.php';
    require_once($yii);
    Yii::createWebApplication($config)->run();
    

    $config gets passed to the web application, which serves as the front controller.

    0 讨论(0)
  • 2020-12-16 06:07

    Actually, index.php should not contain any meaningful code at all, since it would be only part of your site, that is located inside DOCUMENT_ROOT of webserver. It's content should actually look something like:

    <?php 
    
        require '../application/bootstrap.php';
    

    It should only include a file outside DOCUMENT_ROOT. And that's all.

    This way, if something goes horribly wrong (like, php extension fails after server update) and visitors are exposed to raw php code, it will not reveal any sensitive details.

    The point of Front Controller is handle all user input, turn it into a consumable form and, based on it, dispatch a command (usually in a form of method call on an object). In languages like Java, where everything must be contained in a class, a front controller would be a class. But in php you do not have this restriction.

    Instead the front controller will end up being part of your bootstrap stage of the application:

    // --- snip --- 
    // the autoloader has been initialized already a bit earlier
    
    $router = new Router;
    $router->loadConfig($configuration);
    
    $request = new Request;
    $request->setUri($GET['url']); 
    // could also be $_SERVER['PATH_INFO'] or other
    // depends on how url rewrite is set up
    
    $router->route($request);
    // the request instance is populated with data from first matching route
    
    $class = $request->getParameter('resource');
    $command = $request->getMethod() . $request->getParameter('action');
    
    if (class_exists($class)) {
        $instance = new $class;
        $instance->{$command}($request);
        // you dispatch to the proper class's method 
    }
    
    // --- snip --- 
    // then there will be some other code, unrelated to front controller
    

    Also, you should keep in mind that concept of front controller is neither made-for nor demanded-by application that attempt to implement MVC or MVC-inspired architecture.

    0 讨论(0)
  • 2020-12-16 06:10

    You really should read up on the structure of MVC, specifically when used with PHP. Initialize an instance of front-controller in index.php, and it should render your page if that process is part of the front-controller initialization procedure (__constructor()).

    0 讨论(0)
提交回复
热议问题