SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot mr_jeep's Avatar
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    About MVC .... again...

    Hi, I have a question about MVC Pattern.

    I'm wondering if it would be OK that the Model could register some action before being executed and the same thing for the view..

    I mean, every model could have a registerPreAction(&$execList) and registerPostAction(&$execList) method... The view goes the same way.

    Tell me if you don't understand what I'm saying

    Thanks

  2. #2
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you mean tasks to be carried out before and/or after the client page? (If not just ignore the rest of this).

    I think this is exactly why MVC is a confused pattern for web apps: it doesn't express that 3-part structure clearly. Furthermore, in addition to client output, pre/post stages might involve other print-type tasks and hence more (unrelated) models and views to manage.

    I think it's better to embed MVC in web apps, rather than entire apps in MVC. That immediately makes things clearer. Each print-type operation (the client page, obviously, and things like email notification, writing log files etc) can have its own "PrintManager": a sort of quarantine zone preventing MVC from infecting the rest of the app

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think, any pre or post processing that'd you need to do also should pass back to the Controller ?

    Not 100 percent sure on this though, this is the way I'd proberly do it anyway - I cannot understand as to why the View layer would consume pre or post logic, except for maybe POSTing a FORM for example ?

  4. #4
    SitePoint Zealot mr_jeep's Avatar
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    About the view,

    When the view is instanciated by the controller, the controller execute view's registerPreView() and registerPostView() passing the $viewExecutionList object.

    These 2 methods will register some View Objects to be exected before and after the main view. This could be for displaying a header or a footer.

    This is the same thing with the action. The controller will call registerPreFilter() and registerPostFilter's method from the action (model) and stack the actions to be executed in the right order. This could be used to start a database connection or load some things.

    Well, I'm just wondering if this would fit into the MVC pattern applied to the Web and by the way, I'm not quite sure what "Filters" are in a MVC Pattern but I guess they are some executions returning nothing that'S why I used the word Filter.

    Tell me if I'm wrong.

    Thanks

  5. #5
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As far as I know, the MVC pattern has no means of filtering, though there is a filtering pattern for the task you want - just implement this pattern along side the MVC pattern, and it should be ok.

    There was a discussion of filtering here in this forum so maybe searching will turn something up ?

    Btw, could you be willing to post some examples concerning this parts ?

    The controller will call registerPreFilter() and registerPostFilter's method from the action (model) and stack the actions to be executed in the right order.
    I have an idea of what your doing but some examples would help far more

    Thanks again.

  6. #6
    SitePoint Zealot mr_jeep's Avatar
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for answering. I'll be posting some exemple soon (I am currently working on it).

    Thanks again

  7. #7
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your welcome

  8. #8
    SitePoint Zealot mr_jeep's Avatar
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Here's some code

    I finally got some piece of code working and as I said, I'll show you. There is a lot of code.. (well... more PhpDoc Comments than code bu anyway.. you'll have to scroll.) By the way, if somes knows Mojavi, you'll see this code looks like it. I've been greatly inspired by mojavi when I was building this "Framework". Good work Mojavi Team, I don't understand everything yet, but you've build something great.

    Here goes the code, I just pasted what's needed to explain my though (look at preview posts) (I'll explain after and I hope you don't dislike french comments)

    Controller.php :

    PHP Code:
    <?php

    // +---------------------------------------------------------------------------+
    // | Ce fichier fait parti du package Conceptromec                             |
    // | Copyright (c) Conceptromec                                                |
    // |                                                                           |
    // | Pour plus d'informations, veuillez contacter Conceptromec                 |
    // +---------------------------------------------------------------------------+

    /**
     * Objet Controller. Cet objet va analyser la requête courrante, exécuter l'action
     * et va exécuter la bonne vue.
     *
     * @author  Jean-Philippe Déry <jeanphilippe_dery@hotmail.com>
     * @package Conceptromec
     * @version 1.0
     */
    class Controller
    {
        
    /**
         * Objet Request.     
         *
         * @var    object
         * @access private
         * @since  1.0
         */
        
    var $request null;

        
    /**
         * Tableau contenant les informations du controlleur.
         *
         * @var    array
         * @access private
         * @since  1.0
         */
        
    var $infos = array
        (
            
    'current_module' => '',
            
    'current_action' => '',
            
    'request_module' => '',
            
    'request_action' => ''
        
    );

        
    /**
         * Objet User.
         *
         * @var    object
         * @access private
         * @since  1.0
         */
        
    var $user null;

        
    /**
         * Objet requestChain.
         *
         * @var    object
         * @access private
         * @since  1.0
         */
        
    var $requestChain null;
        
        
    /**
         * Constructeur. Créé une instance de tous les objets nécessaires.
         *
         * @access public
         * @since  1.0
         */
        
    function Controller()
        {
            
    $this->request      =& new Request();
            
    $this->user         =& new User();
            
    $this->requestChain =& new RequestChain($this);
        }
        
        
    /**
         * Détermine si une action existe.
         *
         * @param string $modName Le nom du module.
         * @param string $actName Le nom de l'action.
         *
         * @return bool True if the module exists otherwise false.
         *
         * @access public
         * @since  1.0
         */
        
    function actionExists($modName$actName)
        {
            
    $file BASE_DIR "modules/$modName/actions/{$actName}Action.php";
            
            if (
    is_readable($file))
            {
                return 
    true;
            }
            
            return 
    false;
        }
        
        
    /**
         * Return an instance of a given action.
         *
         * @param string $modName Le nom du module.
         * @param string $actName Le nom de l'action.
         *
         * @return object The action's instance.
         *
         * @access public
         * @since  1.0
         */
        
    function & getAction($modName$actName)
        {
            
    $file BASE_DIR $file BASE_DIR "modules/$modName/actions/{$actName}Action.php";
            
            require_once 
    $file;
            
            
    $action =  "{$actName}Action";
            
    $action =& new $action($this$modName$actName);
            
            return 
    $action;
        }    
        
        
    /**
         * Détermine if a view exists.
         *
         * @param string $modName Le nom du module.
         * @param string $actName Le nom de l'action.
         * @param string $actVlue View's type.
         *
         * @return bool True the view exists otherwise false.
         *
         * @access public
         * @since  1.0
         */
        
    function viewExists($modName$actName$actView)
        {
            
    $file BASE_DIR "modules/$modName/views/{$actName}View_{$actView}.php";
            
            if (
    is_readable($file))
            {
                return 
    true;    
            }
            
            return 
    false;
        }

        
    /**
         * Return an instance of a given view.
         *
         * @param string $modName Le nom du module.
         * @param string $actName Le nom de l'action.
         * @param string $actView View's type.
         *
         * @return object Objet View.
         *
         * @access public
         * @since  1.0
         */
        
    function & getView($modName$actName$actView)
        {
            
    $file BASE_DIR "modules/$modName/views/{$actName}View_{$actView}.php";

            require_once 
    $file;
            
            
    $view =  "{$actName}View";
            
    $view =& new $view($this);
            
            return 
    $view;
        }    

        
    /**
         * Retourne l'objet User.
         *
         * @return object Objet User.
         *
         * @access public
         * @since  1.0
         */
        
    function & getUser()
        {
            return 
    $this->user;
        }
        
        
    /**
         * Retoune l'objet Request.
         *
         * @return object L'objet Request
         *
         * @access public
         * @since  1.0
         */
        
    function & getRequest()
        {
            return 
    $this->request;
        }

        
    /**
         * Retourne les informations du controlleur.
         *
         * @return array Les informations du controlleur.
         *
         * @access public
         * @since  1.0
         */
        
    function & getInfos()
        {
            return 
    $this->infos;    
        }

        
    /**
         * Effectue l'action courrante.
         *
         * @access public
         * @since  1.0
         */
        
    function dispatch()
        {
            
    $modName =& $this->request->getParameter('module');
            
    $actName =& $this->request->getParameter('action');
            
            if (
    $modName == null && $actName == null)
            {
                
    $modName DEFAULT_MODULE;
                
    $actName DEFAULT_ACTION;    
            }
            
            
    $this->infos['requestModule'] = $modName;
            
    $this->infos['requestAction'] = $actName;

            
    $this->handle($modName$actName);
        }
        
        
    /**
         * Exécute une action donnée.
         *
         * @param string $modName Le nom du module.
         * @param string $actName Le nom de l'action.
         * 
         * @access public
         * @since  1.0
         */
        
    function handle($modName$actName)
        {
            
    $request      =& $this->request;
            
    $user         =& $this->user;
            
    $infos        =& $this->infos;
            
    $requestChain =& $this->requestChain;
            
            
    $modName preg_replace('/[^a-z0-9_]/i'''trim($modName));
            
    $actName preg_replace('/[^a-z0-9_]/i'''trim($actName));        
            
            if (
    $this->actionExists($modName$actName))
            {
                
    $action =& $this->getAction($modName$actName);
                
                if (
    $action->isSecure())
                {
                    if (!
    $user->isAuthenticated())
                    {
                        if (
    $this->actionExists(AUTH_MODULEAUTH_ACTION))
                        {
                            
    $this->handle(AUTH_MODULEAUTH_ACTION);
                            exit;
                        }
                        
                        
    trigger_error('Cannot find required module : ' AUTH_MODULE ':' AUTH_ACTIONE_USER_ERROR);
                    }
                    
                    if (!
    $user->hasPrivileges($action->getPrivileges()))
                    {
                        if (
    $this->actionExists(ERROR_NO_PRIV_MODULEERROR_NO_PRIV_ACTION))
                        {
                            
    $this->handle(ERROR_NO_PRIV_MODULEERROR_NO_PRIV_ACTION);
                            exit;
                        }
                        
                        
    trigger_error('Cannot find required module : ' ERROR_NO_PRIV_MODULE ':' ERROR_NO_PRIV_ACTIONE_USER_ERROR);    
                    }
                }
                
                
    $action->setPreFilters($requestChain);            
                
    $action->setPostFilters($requestChain);
                            
                
    $requestChain->addRequest($modName$actName$actionREQCHAIN_POS_MIDDLE);
                
    $requestChain->execute();
            }
            else
            {
                if (
    $this->actionExists(ERROR_404_MODULEERROR_404_ACTION))
                {
                    
    $this->handle(ERROR_404_MODULEERROR_404_ACTION);
                    exit;
                }
                
                
    trigger_error('Cannot find required module : ' ERROR_404_MODULE ':' ERROR_404_ACTIONE_USER_ERROR);    
            }
        }
        
        
    /**
         * Gère les erreures.
         *
         * @param int    $flag    Le type d'erreur générée.
         * @param string $message Le message de l'erreur.
         * @param string $file    Le fichier où s'est produit l'erreur.
         * @param int    $line    La ligne où s'est produit l'erreur.
         *
         * @access public
         * @since  1.0
         */
        
    function throwError($flag$message$file$line)
        {
            switch (
    $flag)
            {
                case 
    E_USER_ERROR :
                    echo 
    '<b>ERROR</b> [' $file ' : ' $line '] ' $message '<br/>';
                    break;
                    
                case 
    E_USER_WARNING :
                    echo 
    '<b>WARNING</b> [' $file ' : ' $line '] ' $message '<br/>';
                    break;                        
                
                case 
    E_USER_NOTICE :
                    echo 
    '<b>NOTICE</b> [' $file ' : ' $line '] ' $message '<br/>';
                    break;
                    
                case 
    E_ERROR :
                    echo 
    '<b>ERROR</b> [' $file ' : ' $line '] ' $message '<br/>';
                    break;    
                                
                case 
    E_WARNING :
                    echo 
    '<b>WARNING</b> [' $file ' : ' $line '] ' $message '<br/>';
                    break;

                case 
    E_NOTICE :
                    echo 
    '<b>NOTICE</b> [' $file ' : ' $line '] ' $message '<br/>';
                    break;
                    
                default :
                    echo 
    '<b>UNKNOWN</b> [' $file ' : ' $line '] ' $message '<br/>';
                    break;
            }
        }
    }
    ?>

    Action.php :
    PHP Code:
    <?php

    // +---------------------------------------------------------------------------+
    // | Ce fichier fait parti du package Conceptromec                             |
    // | Copyright (c) Conceptromec                                                |
    // |                                                                           |
    // | Pour plus d'informations, veuillez contacter Conceptromec                 |
    // +---------------------------------------------------------------------------+

    /**
     * Objet Action. Interface de toutes les actions.
     *
     * @author  Jean-Philippe Déry <jeanphilippe_dery@hotmail.com>
     * @package Conceptromec
     * @version 1.0
     */
    class Action
    {
        
    /**
         * Objet Controller.
         *
         * @var    object
         * @access private
         * @since  1.0
         */
        
    var $controller null;

        
    /**
         * Objet Request.
         *
         * @var    object
         * @access private
         * @since  1.0
         */
        
    var $request null;

        
    /**
         * Tableau contenant les informations du controlleur.
         *
         * @var    array
         * @access private
         * @since  1.0
         */
        
    var $infos = array();

        
    /**
         * Objet User.
         *
         * @var    object
         * @access private
         * @since  1.0
         */
        
    var $user null;

        
    /**
         * Objet MySqlDb.
         *
         * @var    object
         * @access private
         * @since  1.0
         */
        
    var $db null;

        
    /**
         * Nom du module.
         *
         * @var    string
         * @access private
         * @since  1.0
         */
        
    var $modName '';

        
    /**
         * Nom de l'action.
         *
         * @var    string
         * @access private
         * @since  1.0
         */
        
    var $actName '';

        
    /**
         * Stack de données pour la vue.
         *
         * @var    array
         * @access private
         * @since  1.0
         */
        
    var $data = array();

        
    /**
         * Constructeur. Instancie les objets nécessaires.
         *
         * @param object $controller Objet Controlleur.
         * @param string $modName    Le nom du module.
         * @param string $actName    Le nom de l'action.
         *
         * @access public
         * @since  1.0
         */
        
    function Action (&$controller$modName$actName)
        {
            
    $this->controller =& $controller;
            
    $this->request    =& $controller->getRequest();
            
    $this->user       =& $controller->getUser();
            
    $this->infos      =& $controller->getInfos();
            
    $this->db         =& MysqlDb::getInstance();

            
    $this->modName $modName;
            
    $this->actName $actName;
            
            
    $this->getLang();
        }

        
    /**
         * Charge tous les mots pour l'action courrante dans l'attribut
         * $this->data['lang'].
         *
         * @access private
         * @since  1.0
         */
        
    function getLang()
        {
            
    $user =& $this->user;
            
    $data =& $this->data;

            
    $language $user->getAttribute('language');

            
    $file BASE_DIR "modules/$this->modName/" .
                               
    "langs/{$this->actName}_{$language}.php";

            if (
    is_readable($file))
            {
                require_once(
    $file);

                if (isset(
    $lang))
                {
                    
    $data['lang'] =& $lang;
                }
            }
        }

        
    /**
         * Enregistre une action qui doit être exécutée avant l'action
         * principale.
         *
         * @param object $requestList Objet RequestList.
         *
         * @access public
         * @since  1.0
         */
        
    function setPreFilters(&$requestList)
        {
            return;
        }

        
    /**
         * Enregistre une action qui doit être exécutée après l'action
         * principale.
         *
         * @param object $requestList Objet RequestList.
         *
         * @access public
         * @since  1.0
         */
        
    function setPostFilters(&$requestList)
        {
            return;
        }

        
    /**
         * Exécute l'action et retourne le type de vue.
         *
         * @return string La constante de la vue.
         *
         * @access public
         * @since  1.0
         */
        
    function execute()
        {
            return 
    VIEW_NONE;
        }

        
    /**
         * Retourne les données pour la vue.
         *
         * @return array The $data array.
         *
         * @access public
         * @since  1.0
         */
        
    function & getData()
        {
            return 
    $this->data;
        }

        
    /**
         * Démterine si l'action est sécure.
         *
         * @return bool True si l'action est sécure sinon false.
         *
         * @access public
         * @since  1.0
         */
        
    function isSecure()
        {
              return 
    false;
        }

        
    /**
         * Retourne les privilèges de l'action.
         *
         * @return mixed Les privilèges sinon null.
         *
         * @access public
         * @since  1.0
         */
        
    function getPrivileges()
        {
            return 
    null;
        }
    }
    ?>
    Request Chain (Actually an action Chain) :

    PHP Code:
    <?php

    // +---------------------------------------------------------------------------+
    // | Ce fichier fait parti du package Conceptromec                             |
    // | Copyright (c) Conceptromec                                                |
    // |                                                                           |
    // | Pour plus d'informations, veuillez contacter Conceptromec                 |
    // +---------------------------------------------------------------------------+

    /**
     * Place l'action donnée dans les premières à être exécutés.
     *
     * @since 1.0
     */
    define('REQCHAIN_POS_BEGIN'0);

    /**
     * Place l'action dans la liste du milieu.
     *
     * @since 1.0
     */ 
    define('REQCHAIN_POS_MIDDLE'1);

    /**
     * Place l'action dans la liste de fin.
     *
     * @since 1.0
     */
    define('REQCHAIN_POS_END'2);

    /**
     * RequestChain Object. Create a list containing actions to execute.
     *
     * @author  Jean-Philippe Déry <jeanphilippe_dery@hotmail.com>
     * @package Conceptromec
     * @version 1.0
     */
    class RequestChain
    {
        
    /**
         * Controller Instance.
         *
         * @var    object
         * @access private
         * @since  1.0
         */
        
    var $controller null;    
        
        
    /**
         * Request Object.
         *
         * @var    object
         * @access private
         * @since  1.0
         */
        
    var $request null;
        
        
    /**
         * Array which contains information about action executions.
         *
         * @var    array
         * @access private
         * @since  1.0
         */
        
    var $infos = array();
        
        
    /**
         * User Instance.
         *
         * @var    object
         * @access private
         * @since  1.0
         */
        
    var $user null;

        
    /**
         * Actions list.
         *
         * @var    array
         * @access private
         * @since  1.0
         */
        
    var $chain = array
        (
            
    REQCHAIN_POS_BEGIN  => array(),
            
    REQCHAIN_POS_MIDDLE => array(),
            
    REQCHAIN_POS_END    => array(),
        );    
            
        
    /**
         * Constructeur.
         *
         * @param object $controller Controller Instance.
         *
         * @access public
         * @since  1.0
         */
        
    function RequestChain($controller)
        {
            
    $this->controller =& $controller;
            
    $this->request    =& $controller->getRequest();
            
    $this->infos      =& $controller->getInfos();
        }
        
        
    /**
         * Add an action to the list.
         *
         * @param string $modName The module's name.
         * @param string $actName The action's name.
         * @param object $action  The action's instance.
         * @param int    $pos     The action's position within the list.
         *
         * @access public
         * @since  1.0
         */
        
    function addRequest($modName$actName, &$action$pos REQCHAIN_POS_MIDDLE)
        {
            
    $this->chain[$pos][] = array
            (
                
    'modName' => $modName,
                
    'actName' => $actName,
                
    'action'  => $action,
            );
        }
        
        
    /**
         * Execute the request chain.
         *
         * @access public
         * @since  1.0
         */
        
    function execute()
        {
            
    $controller =& $this->controller;
            
    $request    =& $this->request;
            
    $user       =& $this->user;
            
    $infos      =& $this->infos;
            
            
    $viewChain  =& new ViewChain();
            
            for (
    $j 0$j 2$j++)
            {
                
    $chain =& $this->chain[$j];
                
                
    $size sizeof($chain);
                
                for (
    $i 0$i $size$i++)
                {
                    
    $modName =& $chain[$i]['modName'];
                    
    $actName =& $chain[$i]['actName'];
                    
    $action  =& $chain[$i]['action'];
                    
                    
    // Update module infos
                    
    $infos['currentModule'] = $modName;
                    
    $infos['currentAction'] = $actName;
                    
                    
    // Execute the given action
                    
    $actView =& $action->execute();
                    
                    if (
    $actView != VIEW_NONE || $actView != null)
                    {
                        if (
    $controller->viewExists($modName$actName$actView))
                        {
                            
    $view =& $controller->getView($modName$actName$actView);
                            
    $view->setData($action->getData());
                            
                            
    $view->setPreFilters($viewChain);
                            
    $view->setPostFilters($viewChain);    
                                                    
                            
    $viewChain->addView($viewVIEWCHAIN_POS_MIDDLE);
                            
    $viewChain->execute();
                        }
                    }
                }
            }
        }
    }
    ?>
    View.php :

    PHP Code:
    <?php

    // +---------------------------------------------------------------------------+
    // | Ce fichier fait parti du package Conceptromec                             |
    // | Copyright (c) Conceptromec                                                |
    // |                                                                           |
    // | Pour plus d'informations, veuillez contacter Conceptromec                 |
    // +---------------------------------------------------------------------------+

    /**
     * Error View.
     *
     * @since 1.0
     */
    define('VIEW_ERROR''error');

    /**
     * Common View.
     *
     * @since 1.0
     */
    define('VIEW_INDEX''index');

    /**
     * Form View.
     *
     * @since 1.0
     */
    define('VIEW_INPUT''input');

    /**
     * No View.
     *
     * @since 1.0
     */
    define('VIEW_NONE''');

    /**
     * Success View.
     *
     * @since 1.0
     */
    define('VIEW_SUCCESS''success');

    /**
     * View Object. Interface of every actions.
     *
     * @author  Jean-Philippe Déry <jeanphilippe_dery@hotmail.com>
     * @package Conceptromec
     * @version 1.0
     */
    class View
    {
        
    /**
         * Controller Instance.
         *
         * @var    object
         * @access private
         * @since  1.0
         */
        
    var $controller null;    
        
        
    /**
         * Request Object.
         *
         * @var    object
         * @access private
         * @since  1.0
         */
        
    var $request null;
        
        
    /**
         * Array which contains information about action executions.
         *
         * @var    array
         * @access private
         * @since  1.0
         */
        
    var $infos = array();
        
        
    /**
         * User Instance.
         *
         * @var    object
         * @access private
         * @since  1.0
         */
        
    var $user null;
        
        
    /**
         * Objet template utilisé pour charger les fichiers templates
         *
         * @var    object
         * @access protected
         * @since  1.0
         */
        
    var $template null;
        
        
    /**
         * Données provenant du model.
         *
         * @var    array
         * @access protected
         * @since  1.0
         */
        
    var $data = array();

        
    /**
         * Créé une nouvelle instance d'une vue.
         *
         * @param object $controller L'objet Controller.
         *
         * @access public
         * @since  1.0
         */
        
    function View (&$controller)
        {
            
    $this->controller =& $controller;
            
    $this->request    =& $controller->getRequest();
            
    $this->user       =& $controller->getUser();
            
    $this->infos      =& $controller->getInfos();
            
    $this->template   =& new Template();
        }
        
        
    /**
         * Assigne le fichier template à utiliser.
         *
         * @param string $file Le fichier template a charger
         *
         * @access public
         * @since  1.0
         */
        
    function setTemplate($file)
        {
            
    $template =& $this->template;
            
    $infos    =& $this->infos;

            
    $path BASE_DIR "modules/{$infos['currentModule']}/templates/$file";
            
            if (
    is_readable($path))
            {
                
    $template->load($path);
            }
        }
        
        
    /**
         * Assigne les données de la vue.
         *
         * @param array $data Les données du model.
         *
         * @access public
         * @since  1.0.
         */
        
    function setData(&$data)
        {
            
    $this->data $data;
        }
        
        
    /**
         * Enregistre une action qui doit être exécutée avant l'action
         * principale.
         *
         * @param object $viewList Objet RequestList.
         *
         * @access public
         * @since  1.0
         */
        
    function setPreFilters(&$viewList)
        {
            return;
        }

        
    /**
         * Enregistre une action qui doit être exécutée après l'action
         * principale.
         *
         * @param object $viewList Objet RequestList.
         *
         * @access public
         * @since  1.0
         */
        
    function setPostFilters(&$viewList)
        {
            return;
        }    
        
        
    /**
         * Exécute la vue et l'affiche a l'écran.
         *
         * @param object $action L'objet action.
         *
         * @access public
         * @since  1.0
         */
        
    function execute()
        {

        }  
    }
    ?>
    ViewChain :

    PHP Code:
    <?php

    // +---------------------------------------------------------------------------+
    // | Ce fichier fait parti du package Conceptromec                             |
    // | Copyright (c) Conceptromec                                                |
    // |                                                                           |
    // | Pour plus d'informations, veuillez contacter Conceptromec                 |
    // +---------------------------------------------------------------------------+

    /**
     * Place l'action donnée dans les premières à être exécutés.
     *
     * @since 1.0
     */
    define('VIEWCHAIN_POS_BEGIN'0);

    /**
     * Place l'action dans la liste du milieu.
     *
     * @since 1.0
     */ 
    define('VIEWCHAIN_POS_MIDDLE'1);

    /**
     * Place l'action dans la liste de fin.
     *
     * @since 1.0
     */
    define('VIEWCHAIN_POS_END'2);

    /**
     * Objet ViewChain. Cet objet permet d'exécuter plusieurs vue qui sont stoqués
     * dans une file.
     *
     * @author  Jean-Philippe Déry <jeanphilippe_dery@hotmail.com>
     * @package Conceptromec
     * @version 1.0
     */
    class ViewChain
    {
        
    /**
         * Liste des vues a exécuter
         *
         * @var    array
         * @access private
         * @since  1.0
         */
        
    var $chain = array
        (
            
    VIEWCHAIN_POS_BEGIN   => array(),
            
    VIEWCHAIN_POS_MIDDLE  => array(),
            
    VIEWCHAIN_POS_END     => array(),
        );
        
        
    /**
         * Ajoute une vue à la chaine d'exécution.
         *
         * @param ojbect $view Un objet View.
         *
         * @access public
         * @since  1.0
         */
        
    function addView(&$view$pos VIEWCHAIN_POS_MIDDLE)
        {
            
    $this->chain[$pos][] = $view;
        }
        
        
    /**
         * Exécute la liste des vues.
         *
         * @access public
         * @since  1.0
         */
        
    function execute()
        {
            for (
    $j 0$j 2$j++)
            {
                
    $chain =& $this->chain[$j];
                
                
    $size sizeof($chain);
                
                for (
    $i 0$i $size$i++)
                {
                    
    $chain[$i]->execute();
                }
            }
        }
    }

    ?>

    Ok this is the base class, now for the classes that make a "blank" page. (Page with navigation, header, footer but nothing in the middle.. yet)

    IndexAction.php
    PHP Code:
    <?php

    // +---------------------------------------------------------------------------+
    // | Ce fichier fait parti du package Conceptromec                             |
    // | Copyright (c) Conceptromec                                                |
    // |                                                                           |
    // | Pour plus d'informations, veuillez contacter Conceptromec                 |
    // +---------------------------------------------------------------------------+

    /**
     * Objet IndexAction. Buisness Logic de la page d'index.
     *
     * @author  Jean-Philippe Déry <jeanphilippe_dery@hotmail.com>
     * @package Conceptromec
     * @version 1.0
     */
    class IndexAction extends Action 
    {
        
    /**
         * Constructeur. Instancie les objets nécessaires.
         *
         * @param object $controller Objet Controlleur.
         * @param string $modName    Le nom du module.
         * @param string $actName    Le nom de l'action.
         *
         * @access public
         * @since  1.0
         */
        
    function IndexAction (&$controller$modName$actName)    
        {
            
    parent::Action($controller$modName$actName);
        }    
        
        
    /**
         * Exécute l'action.
         *
         * @return string La constante de la vue à utiliser.
         *
         * @access public
         * @since  1.0
         */
        
    function execute()
        {
            return 
    VIEW_INDEX;
        }
      
        
    /**
         * Cette action ne nécessite pas de sécurité.
         *
         * @return bool False car l'action ne nécessite pas de sécurité.
         *
         * @access public
         * @since  1.0
         */
        
    function isSecure()
        {
              return 
    false;      
        }
        
        
    /**
         * Cette page ne nécessite aucun privilèges spéciaux.
         *
         * @return null Null car aucuns privilèges n'est requis.
         *
         * @access public
         * @since  1.0
         */
        
    function getPrivileges()
        {
            return 
    null;    
        }    
    }
    ?>
    IndexView_index.php
    PHP Code:
    <?php

    // +---------------------------------------------------------------------------+
    // | Ce fichier fait parti du package Conceptromec                             |
    // | Copyright (c) Conceptromec                                                |
    // |                                                                           |
    // | Pour plus d'informations, veuillez contacter Conceptromec                 |
    // +---------------------------------------------------------------------------+

    require_once BASE_DIR 'modules/Default/views/Default.php';

    /**
     * Objet View. Objet de base pour toutes vues.
     *
     * @author  Jean-Philippe Déry <jeanphilippe_dery@hotmail.com>
     * @package Conceptromec
     * @version 1.0
     */
    class IndexView extends View 
    {
        
    /**
         * Créé une nouvelle instance d'une vue.
         *
         * @param object $controller L'objet Controller.
         *
         * @access public
         * @since  1.0
         */
        
    function IndexView (&$controller)
        {
            
    parent::View($controller);
        }
        
        
    /**
         * Enregistre une action qui doit être exécutée avant l'action
         * principale.
         *
         * @param object $viewList Objet RequestList.
         *
         * @access public
         * @since  1.0
         */
        
    function setPreFilters(&$viewList)
        {
            require_once 
    BASE_DIR 'modules/Default/viewFilter/HeaderViewFilter.php';
            
            
    $headerView =& new HeaderViewFilter($this->controller);
            
    $headerView->setData($this->data);
            
            
    $viewList->addView($headerView);
        }

        
    /**
         * Enregistre une action qui doit être exécutée après l'action
         * principale.
         *
         * @param object $viewList Objet RequestList.
         *
         * @access public
         * @since  1.0
         */
        
    function setPostFilters(&$viewList)
        {
            return;
        }     
        
        
    /**
         * Exécute la vue et l'affiche a l'écran.
         *
         * @access public
         * @since  1.0
         */
        
    function execute()
        {
            
    $controller =& $this->controller;
            
    $request    =& $this->request;
            
    $user       =& $this->user;
            
    $infos      =& $this->infos;
            
    $template   =& $this->template;
            
    $lang       =& $this->data['lang'];
            
    $data       =& $this->data;

            
    $this->setTemplate('Index_index.tpl');
            
                    
            
    $template->pparse();
        }
    }
    ?>
    HeaderViewFilter.php :
    PHP Code:
    <?php

    // +---------------------------------------------------------------------------+
    // | Ce fichier fait parti du package Conceptromec                             |
    // | Copyright (c) Conceptromec                                                |
    // |                                                                           |
    // | Pour plus d'informations, veuillez contacter Conceptromec                 |
    // +---------------------------------------------------------------------------+

    require_once BASE_DIR 'modules/Default/views/Default.php';

    /**
     * Objet HeaderViewFilter
     *
     * @author  Jean-Philippe Déry <jeanphilippe_dery@hotmail.com>
     * @package Conceptromec
     * @version 1.0
     */
    class HeaderViewFilter extends View 
    {
        
    /**
         * Créé une nouvelle instance d'une vue.
         *
         * @param object $controller L'objet Controller.
         *
         * @access public
         * @since  1.0
         */
        
    function HeaderViewFilter (&$controller)
        {
            
    parent::View($controller);
        }
        
        
    /**
         * Enregistre une action qui doit être exécutée avant l'action
         * principale.
         *
         * @param object $requestList Objet RequestList.
         *
         * @access public
         * @since  1.0
         */
        
    function setPreFilters(&$requestList)
        {
            return;
        }

        
    /**
         * Enregistre une action qui doit être exécutée après l'action
         * principale.
         *
         * @param object $requestList Objet RequestList.
         *
         * @access public
         * @since  1.0
         */
        
    function setPostFilters(&$requestList)
        {
            return;
        }    
        
        
    /**
         * Exécute la vue et l'affiche a l'écran.
         *
         * @param object $action L'objet action.
         *
         * @access public
         * @since  1.0
         */
        
    function execute()
        {
            
    $this->setTemplate('Header.tpl');
            
            
    // Raccourcis
            
    $controller =& $this->controller;
            
    $request    =& $this->request;
            
    $user       =& $this->user;
            
    $infos      =& $this->infos;
            
    $template   =& $this->template;
            
    $data       =& $this->data;
            
    $lang       =& $this->data['lang'];


            
    // Ajoute les valeurs dans le template
            
    $template->assignVars(array
            (
                
    'L_CONCEPTROMEC'      => $lang['Conceptromec'],
                
    'L_HOME'              => $lang['Home'],
                
    'L_PRODUCTS'          => $lang['Products'],
                
    'L_NEWS'              => $lang['News'],
                
    'L_CAREERS'           => $lang['Careers'],
                
    'L_CONTACT_US'        => $lang['Contact_Us'],
                
    'L_GENERAL'           => $lang['General'],
                
    'L_SALES'             => $lang['Sales'],
                
    'L_INGENEERING'       => $lang['Ingeneering'],
                
    'L_SUPPORT'           => $lang['Support'],
                
    'L_ABOUT_US'          => $lang['About_Us'],
                
    'L_HISTORY'           => $lang['History'],
                
    'L_MISSION'           => $lang['Mission'],
                
    'L_CLIENT'            => $lang['Client'],
                
    'L_LOST_PASSWORD'     => $lang['Lost_Password'],
                
    'L_PROJECTS'          => $lang['Projects'],

                
    'HEADER_DESCRIPTION'  => HEADER_DESCRIPTION,
                
    'HEADER_KEYWORDS'     => HEADER_KEYWORDS,
                
    'HEADER_CONTENT_TYPE' => HEADER_CONTENT_TYPE,
                
    'HEADER_CHARSET'      => HEADER_CHARSET,

                
    'BASE_DIR'            => BASE_DIR,
            ));

            
    // Menus Login/Logout
            
    if ($user->isAuthenticated())
            {
                
    $template->assignVars(array
                (
                    
    'L_SIGN_OUT' => $lang['Sign_Out'],
                    
    'L_PROFILE'  => $lang['Profile'],
                ));
            }
            else
            {
                
    $template->assignVars(array
                (
                    
    'L_SIGN_IN'  => $lang['Sign_In'],
                    
    'L_REGISTER' => $lang['Register'],
                ));
            }        
            
            
    $template->pparse();
        }
    }
    ?>
    Ok this is done now. I'll explain.

    The Controller handle the request, since the IndexAction occurs when there is no request given, let's assume no request has been given.

    into : Controller::handle(), The action (model) is instanciated and both methods setPreFilter() and setPostFilters are called. These method will be overriden within other classes to provide other possibles action to be executed before and after the main action.

    When the main action (model) is executed, it returns a view name. From this view, the controller select the View class to execute (well just like Mojavi). Then the view is instanciated and both methods setPreFilters() and setPostFilters() which will be overriden on the classes. In the example, the method setPreFilters() from IndexAction Class will add the class HeaderViewFilter which generate a html header.

    So, before generating the main page view, the header will be printed out.

    I hope this is helping you to understand, I'll understand if you don't read everything because, indeed, there is a LOT of stuff here and you need a lot of time and interests to go trough .

    Thanks for reading and now, if you can, tell me this thing fit into MVC pattern.

    Thanks.

    P.S. Just some note. About the word "Filter", when I studied Mojavi, the filter thing was not absolutly clear to me. I figured it was juste some piece of execution to be run before an action (model). I may be wrong. Oh, yhea, something else. in the words setPreFilters and setPostFilters, the words "post" has nothing to do with $_POST or "method="post" values. they means before, after... Just in case (I'm not kicking *** when writing in english. Hope you can understand)
    I'd appreciate if you would tell me if something I wrote is spelled wrong

  9. #9
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahem

    That is a lot of script you have there Thanks for posting it. For the most part, looking very briefly over it, it does look like it fits into the MVC pattern.

    Need more time to really look at it though to say for sure. I done a basic FC before though I found the whole process wasteful to me anyway, now I'm looking at PC (Page Controller) instead just now.

    To me a PC does have the advantage of scripting on a per page level, ie

    PHP Code:
    news.php
    product
    .php 
    for example, as apposed to directing everything via

    PHP Code:
    index.php 
    But anyways, thanks for posting your script, which I'm going to look at over the weekend if I can, and see what comments I can then make ?

  10. #10
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    Sydney
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeh im using Page Controllers over a Front Controller on the current project im working on. I do enjoy it much more.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •