There are probably many ways, but how to do it best? Has anyone done any general page name structures? You could probably use the name of the controller, for example, but this is not enough. The controller is one, and the pages can be different. it would be nice to set the page name directly in the view. Or in action . Anyway, did anyone do that?

    2 answers 2

    Right way - to transfer to the view parameter with the name of the page. From where you will take it - absolutely no difference. From the database, from PHP, from a text file - it's up to you.

    • Yes, very similar to the right way - heleg

    And you can be more precise about what is generally needed?

    If it is just pages or articles by section.

    What is the problem?

    make the controller page model for processing from the staticpages table is valid.

    in the config, you can redirect the pages immediately to view by alias from the page name and not by the record ID.

     'urlManager'=>array( 'urlFormat'=>'path', 'rules'=>array( 'page/<page:\w+>'=>'page/view', ), ), 

    In the controller, let's say that the thread is the same

     public function actionView() { $criteria = new CDbCriteria; $criteria -> select = '*'; $criteria -> condition = 'alias=:alias'; if (!isset($_GET['page'])) $_GET['page'] = 'main'; $criteria -> params = array(':alias' = > $_GET['page']); $find_page = StaticPages::model () -> find($criteria); if ($find_page != NULL) $this -> render('view'); else { $this -> breadcrumbs = array('404'); $this -> content = '404 page not found'; $this -> redirect('/page/404'); $this -> render($this -> layout); } } 

    Well and everything, under the condition of the typed address of the page / about form we climb in the DB, we look at the page alias, if we show what we need, if not, then redirect 404 or just output an error.

    • yes, there may be no problem. I just started to learn yii, and I'm afraid to start applying my bikes to him. Now I will understand your example. - heleg
    • No, this is not about static pages, but about pages in general. for example, the main page id will be index, and all elements of this page can be identified in css as #index .my_element. on the contacts page this item will be called #contacts .my_element. This does not mean that the main page and contact pages are static. pages have id regardless of how they are generated - heleg
    • for each element, no one bothers to add alias from the database - but why. - Artem
    • alias is not an element, but a page - heleg
    • Is everything okay with logic? >. on the contact page, this element will be called #contacts .my_element. And then you write the opposite> alias not to the element, but to the page I don’t understand you at all. Understand in your head what is needed and what is not, and how it should be. and answer your questions! - Artem