URL beautification of the module or module style controller in the Yii Framework

Source: Internet
Author: User
URL beautification of the module or module style controller in the Yii Framework

Since I have been writing a blog system similar to Tumblr in my spare time recently, many models, controllers, and views are involved, such as the user module, article module, image module, music module, Video module, and thing (commodity) module. Each module has its own tag, comment, category, and image modules, in this way, if the controller and view are separately opened, they will be very bloated, complicated, and difficult to manage and maintain. However, they do not want to modularize their functions because their functions are relatively light for the time being, therefore, you are ready to use a module-style folder to manage controllers and views. The style is similar to the following:

Controller (take the article as an example)

View (take the article as an example)

In the above controller, IndexController corresponds to the original ArticleController, because in order to facilitate unified matching in urlManager, the main controller name of all modules is set to IndexController, for example, MusicController is IndexController, and so on. Change ArticleCategoryController to CategoryController, and so on,

In this way, you can access the create method of category in the URL, such as article/category/create, currently, the URL manager is configured to still be accessed in the form of article/create, specifically in protected/config/main. set urlManager in php as follows:

/*** Author: Farm farm * url: http://www.icultivator.com/p/6168.html * website: http://www.icultivator.com */'urlmanager' => array ('urlformat' => 'path ', 'rules' => array (''=> 'site/Index','' => 'site /','
 
  
'=>'
  
   
/Index/Index ','
   
    
/
    
     
'=>'
     
      
/Index/view ','
      
        //
       
         '=>'
        
          /Index /','
         
           /Tag/
          
            '=>'
           
             /Index/tag ','
            
              /'=>'
             
               /Index /','
              
                /
               
                 /
                
                  '=>'
                 
                   /
                  
                    /View ','
                   
                     /
                    
                      //
                     
                       '=>'
                      
                        /
                       
                         /','
                        
                          /
                         
                           /'=>'
                          
                            /
                           
                             /',), 'Showscriptname' => FALSE ,),
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 

In this way, you can achieve unified URL Beautification. for example, you can view articles with the id of article 1, which can be accessed through article/1, articles whose tag is test can be accessed through article/tag/test.

In addition, you must change the original render url path to // article/in each IndexController (taking article as an example). In the view, the article controller corresponds to each view file, you also need to modify the corresponding url path to beautify the URL. if you do not want to modify the path in the render, you can put the view file in the original article to the/views/article/index folder. The principle is that if the path in render contains //, the corresponding url will be parsed to/path/to/views. "/". article/view, otherwise it will be parsed to/path/to/views/article/index. "/". view. For more information about the reason, see the Yii core source file. In short, it is about calling the Yii: app ()-> getViewPath () and $ this-> getViewPath () methods, the view file obtained by the former is obtained through Yii: app ()-> getViewPath (). "/". $ viewName, which obtains the view file through $ this-> getViewPath.

Of course, if you are interested, you can also inherit the CBaseUrlRule class to customize a UrlRule class for URL generation and resolution. for details, see this article: yii Framework official guide series 43-topics: URL (creation, routing, beautification and customization ). I will not go into details here. For URL management in the Yii Framework, refer to this article: Yii Framework Analysis (8)-URL management component.

If you have any questions, please refer to the reply below.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.