Where to implement knowledge about the addresses of the controller?

  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.


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.


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());
        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')
                // 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')
                    // 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
        ->setParam('id', $product->id)

    // address string ?r=site/product&id=1

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

Source Code

Source Code Url Builder