Where to implement knowledge about the addresses of the controller?

You are viewing revision #2 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#3) »

  1. Statement of problem
  2. Solution
  3. Examples of use
  4. Source Code

Statement of problem

Knowledge is necessary to determine the address of the controller in a single layer system. This allows you to quickly search and painlessly produce refactoring controllers and their addresses.

UPD: In order to show the problem more clearly, we can give the following example, when the addresses specified in the system controllers wherever you want, in the controllers, in presentations, sometimes even in client script, and this process becomes unmanageable.

Implement checking adequacy parameters for constructing address. This is necessary if the parameters are set for an address in another layer of the system, such as the representation of a client or a script.

Solution

All addresses must be defined in the controllers. If necessary, you can fill in missing parameters in the presentation layer, or client script.

For convenient operation can determine helper - Builder addresses.

http://habrastorage.org/getpro/habr/post_images/284/710/479/28471047983d6d9f9b42eda993a1ed0b.png

Examples of use

Definition of knowledge about the address in the controller

Abstract base controller. Implementation of the method of creating properties per Builder addresses

class BaseController extends \CController {

    public function createUrlBuilder($route, $params = array()) {
        $urlBuilder = new UrlBuilder($this->getUrlManager());
        $urlBuilder
            ->setRoute($route)
            ->setParams($params);
        return $urlBuilder;
    }

    public function getUrlManager() {
        $urlManager = $this->getApp()->getUrlManager();
        return $urlManager;
    }

    public function getApp() {
        return \Yii::app();
    }
}

Concrete controller. Using Builder addresses

class SiteController extends BaseController {

    public function actionIndex() {
        return $this->render('index', array(
            'urls' => array(
                'catalog' => $this->createUrlBuilder('site/catalog')
                    ->getUrl(),
                // ready assigned address line ?r=site/catalog
            ),
        ));
    }

    public function actionCatalog() {
        return $this->render('catalog', array(
            'products' => Product::model()->findAll(),
            'urls' => array(
                'product' => $this->createUrlBuilder('site/product')
                    ->setRequired(array('id')),
                    // passed to the builder with the necessary knowledge about the controller, 
                    // required parameters are defined, which are filled in the representation
            ),
        ));
    }

    public function actionProduct($id) {
        return $this->render('product');
    }
}

Presentation of output product catalog (catalog.php)

/** @var UrlBuilder $productUrlBuilder */
$productUrlBuilder = $this->getParam('urls.product');

foreach ($this->getParam('products') as $product) {
    $productUrl = $productUrlBuilder
        ->copy()
        ->setParam('id', $product->id)
        ->getUrl();

    print($productUrl);
    // address string ?r=site/product&id=1
}

// or pass parameters url builder the client script
$this->setJsParams(array(
    'urls' => array(
        'product' => $productUrlBuilder->toArray(),
    ),
));

Source Code

Source Code Url Builder

1 0
2 followers
Viewed: 51 955 times
Version: Unknown (update)
Category: Tutorials
Written by: Petr.Grishin
Last updated by: Petr.Grishin
Created on: Jun 19, 2014
Last updated: 10 years ago
Update Article

Revisions

View all history

Related Articles