SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Controller vs Router

    I'm trying to build an Admin system and I'm getting stuck on the login section.

    If I use a router i need to to do somthing like this to secure user access:

    PHP Code:
    <?php
    // Router
    $router =& $locator->get('router');

    // Controller Directory
    $router->setDirectory(DIR_CONTROLLER);

    // Default Controller
    $router->setDefault('home');

    // Error Controller
    $router->setError('error_not_found');

    // Login
    if ((!$user->isLogged()) && ($request->get('controller') != 'login')) {
        
    $response->redirect($url->ssl('login'));
    }

    // Dispatch
    if ((!$user->hasPermission('access'$request->get('controller'))) && ($request->get('controller') != 'login')) {
        
    $response->redirect($url->ssl('error_permission'));
    }

    $router->dispatch($request->get('controller'), $request->get('action'));
    ?>
    If I use a controller I can use addPreMethods:

    PHP Code:
    <?php
    // Controller
    $controller =& $locator->get('controller');

    // Controller Directory
    $controller->setDirectory(DIR_CONTROLLER);

    // Default Controller
    $controller->setDefault('home');

    // Error Controller
    $controller->setError('error_not_found');

    // Login
    $controller->addPreMethod('login''logged');

    // Permission
    $controller->addPreMethod('login''permission');

    // Dispatch
    $controller->dispatch($request->get('controller'), $request->get('action'));
    ?>
    Can someone please advise which is going to be the best solution for me? I would also want to keep the ability of the router for search engine optimization on the catalog side.

  2. #2
    <?php while(!sleep()){code();} G.Schuster's Avatar
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why do you think you can't use your pre-methods with a router?
    The router should normally be designed that it only parses a string into a controller and set the environment exactly as it would be when your URL already was formed in your "controller-only" model.

    It's hard to give you a real advice as long as we don't have exact code showing e.g. the addPreMethod.

  3. #3
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by G.Schuster View Post
    Why do you think you can't use your pre-methods with a router?
    The router should normally be designed that it only parses a string into a controller and set the environment exactly as it would be when your URL already was formed in your "controller-only" model.

    It's hard to give you a real advice as long as we don't have exact code showing e.g. the addPreMethod.

    I always thought the router was for routing the request to the correct controller. If its for converting a string and fed to the base controller I should be able to use pre methods in the controller.

  4. #4
    <?php while(!sleep()){code();} G.Schuster's Avatar
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can implement it the way you need, no need to stick to any paradigm.

  5. #5
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi guys, according to Zend and other frameworks:

    Routing is the process of taking a URI endpoint (that part of the URI which comes after the base URL) and decomposing it into parameters to determine which module, controller, and action of that controller should receive the request.

    This values of the module, controller, action and other parameters can be packaged into a Request_Http object and then processed by the Dispatcher.

    Routing occurs only once, when the request is initially received and before the first controller is dispatched.

    Now, base on the URL path you can figure out which controller has been requested, if it's a back-end controller, save this info into the Request_Http object and then let the Dispatcher decide for you.


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
  •