I have quite a trivial (I think the question). I recently started to learn Yii and I can’t figure it out yet. The point is this: We are developing a site with 2 types of pages:

  1. “Static”, whose call is specified by actionMethod () methods in the corresponding controller. For them, there are views, and they reflect the basic structure of the site (in the main menu). URL: mysite.ru/product/
  2. Added by admin through backend-interface. They are subsections of the main sections and may be present in the submenu of the main menu. URL: mysite.ru/product/osago

... where osago is the alias of the type of product in the database. In fact, neither the osago page nor the folder exists. After extracting data from the database, they should be placed in a template. The challenge is that when calling the productController controller, it does not send us on page 404. We cannot have an actionMethod for each type of product, since they can be added at any time. Accordingly, it should:

  1. Check for actionMethod () in the controller. If found, acts in the usual manner, i.e., calls this method.

  2. If not found, search for alias extracted from the URL in the database. If found, then get the data and place in the template for the product.

  3. If not found, send on p. 404.

Actually, it is not quite clear how to fulfill condition 2. Surely, the task is standard for those who are well acquainted with Yii. I would be very grateful to my dear colleagues for the answers on the merits. Thank!

    2 answers 2

    Tin. Do we understand how MVC works?

    There is a product control in your case.


    It should be so, but after the product should go action

    those. it turns out so


    those. somewhere described

     public function actionView() { /* Что-то делаем, допустим ищем в БД нужную запись по $_GET['name']*/ $this->render('view'); } 

    But do not like the record so big


    I want shorter


    Then to the project settings file

     'urlManager'=>array( 'urlFormat'=>'path', 'showScriptName'=>false, // показывать имя скрипта!!! 'rules'=>array( 'product/<name:\w+>'=>'product/view', ), ), 

    Well, that's all, now you do all the URLs on the whole site, mysite.ru/product/osago, and he sort of sends to mysite.ru/product/view/name/osago and that's it.

    • Shrek, you are driving. However, understanding MVC is not to blame. Here you need an understanding of the work of Yii, because the urlManager specified here, it is his method. I found the solution a little earlier, but it is practically the same as what you proposed. Respect and all that. - srgg67
    • @ srgg67 No, it’s about understanding MVC / Understanding how any framework works, be it zend, yui, igniter, they are basically identical. Methods are specified in controls; nobody creates them dynamically. Accordingly, it is here that everything rests on processing the data we need from the address bar. But here come the rules for handling URLs. - Artem

    It seems to me that you are already doing wrong from the very beginning, I yii not familiar with yii , but intercepting actionMethod is a bad idea, and it is even more stupid to create separate actions for each subdirectory: the user did not find any product, catalog or article in the database, then output he is told that there is no such product.

    The task is that when calling the productController controller it does not productController us on page 404

    Since I'm far from what is happening in yii , but in the same Kohana it's all up to your perforations with directories to be configured in the bootsrap . I think that in yii should be something like that.

    • *** The user has found a product ... it means displaying a message to him that there is no such product. It will work in the same way. This is about cases when content is in the database, but Yii cannot find a method whose name corresponds to action retrieved from the URL. The task is trivial. Conceptually solved in all (if not mistaken) CMS. In essence, this is the same when the user creates a page with the article himself. - srgg67