Frage

I have a route like this:

resources.router.routes.home.route = /:state/:city
resources.router.routes.home.defaults.module = default
resources.router.routes.home.defaults.controller = index
resources.router.routes.home.defaults.action = index
resources.router.routes.home.defaults.state = ''
resources.router.routes.home.defaults.city = ''

And most of the links from my site need these two parameters (state and city), like

www.mysite.com/sp/sao-paulo/establishments
www.mysite.com/sp/sao-paulo/establishment-name
www.mysite.com/sp/sao-paulo/establishments-category

What I need is to check if this two parameters are already set when the user accesses my website. If not, I'll redirect him to a specific page when he'll choose a city.

I believe the best way to achieve this is by creating a plugin, which I've already started.

class App_Controller_Plugin_CheckCity extends Zend_Controller_Plugin_Abstract {

  protected $_session;

  public function preDispatch(Zend_Controller_Request_Abstract $request) {

    $this->_session = new Zend_Session_Namespace('locationInformation');

    if (!isset($this->_session->state) || !isset($this->_session->city)) {
      // ...
    } else {
      // ...
    }

  }

}

The problem is I don't know exactly how to do it, and if this is really the best way.

Is this the correct path? If not, what can I do to solve my problem? If yes, what else this plugin needs?

I hope I made myself clear. I appreciate any help.

Thank you.

War es hilfreich?

Lösung

Considering that you'll not have a default state/city, then the first thing the user will see when accessing the website is the page where he'll choose his location, no matter which link he tries to access.

If that's the case, then you need to write a plug-in that will check if those options are already set by the user:

<?php
class App_Controller_Plugin_CheckCity extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        // Gets the session
        $session = new Zend_Session_Namespace('locationInformation');

        // Checks if the state/city are set and
        // if not set, checks if those params are set in the URI
        if( (!isset($session->state) || !isset($session->city))
            && ($request->has('state') && $request->has('city')) ) {
            // If yes, then saves it in the session
            $session->state = $request->getParam('state');
            $session->city = $request->getRequest()->getParam('city');
        } elseif(!isset($session->state) || !isset($session->city)) {
            // If not, forward the user to the controller/page where he'll choose his location
            $request->setControllerName('location')
                    ->setActionName('choose-location')
                    ->setModuleName('default');
        }

        if(isset($session->state) && isset($session->city)) {
            // Sets the params for use within your views
            $view = Zend_Registry::get('view');
            $view->state = $session->state;
            $view->city = $session->city;
        }
    }
}

After choosing it, you'll redirect the user again to the page he just tried to access. To do so, you'll grab the URI on the state/city page (LocationController):

$session->redirectTo = 'http://' . $this->getRequest()->getHttpHost() . $this->getRequest()->getRequestUri();

After setting the user's location, you'll use this to perform the redirection within your LocationController:

$this->_redirect($session->redirecTo);



UPDATE

In order to have access to your view object everywhere saves it in the registry:

/**
 * Initializes the layout and view
 */
protected function _initView() {
    /**
     * @var \Zend_View $view
     * @var \Zend_Layout $layout
     */
    $layout = $this->bootstrap('layout')->getResource('layout');
    $view = $layout->getView();

    $view->doctype('XHTML1_STRICT');
    $view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');

    // Adds to the registry
    Zend_Registry::set('view', $view);
}

Check the plug-in code again to see how to set the variables in the view.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top