A
A
Alexander Shapovalov2016-04-08 09:10:22
Yii
Alexander Shapovalov, 2016-04-08 09:10:22

How to add custom CNCs using Yii framework?

Hello, there is a problem that I solved vlob, if there is a simpler and more elegant solution, I would be happy to help! There is a link field in the table where the name of the link after the domain is set, for example, http://site/data-from-link-variable i.e. this field is unique and the material is pulled from the database by the value of this field. I implemented it this way, I set up the main.php file in the config directory by redirecting requests to one controller that parses them all, because. there are more tables in the database that store data with previously set links, you have to do an additional check.
(Updated, implemented like this, everything is sorted out in one controller, Cyrillic links work through the utf8_encode function, it is possible to set almost any characters)
in the config file:

'urlManager'=>array(
      'urlFormat' => 'path',
      'showScriptName' => false,
      'caseSensitive' => true, 
      'useStrictParsing'=>true,
      'urlSuffix'=>'/',
      'rules' => array(
      
        'admin/<_c>/<_a>/*' => 'admin/<_c>/<_a>',
        'gii/<_c>/<_a>/*' => 'gii/<_c>/<_a>',
        'gii/*' => 'gii/',
        'admin/*' => 'admin/',
        'site/<_a>/*' => 'site/<_a>',
        '<link:[\w\W]+>' => 'content/get',
        '/*' => '',
        

      ),      
    ),

in controller:
public function actionGet($link)
  {
      $url=utf8_encode($_SERVER['REQUEST_URI']);
      
      $model=Categories::model()->findBySql('SELECT * FROM tbl_categories WHERE link=:query',array(':query'=>trim($url)));
      
      if($model!=NULL)
      {
        $data=Content::model()->findBySql('SELECT * FROM tbl_content WHERE category=:cat',array(':cat'=>$model->id));
        
        $this->render('view',array(
        'model'=>$model,
        'data'=>$data
        ));
      }
      else
      {
      $model=Content::model()->findBySql('SELECT * FROM tbl_content WHERE link=:query',array(':query'=>trim($url)));

      if($model!=NULL)
      {
        $this->render('view',array(
        'model'=>$model,
        ));
      }
      else
      throw new CHttpException(404,'The requested page does not exist.');
        
    }
    
    

  }

Is there an easier way to hook up custom CNCs to Yii?
The question arose in connection with the translation of the old site with thousands of materials and links to them to Yii, the old site was parsed and the task is to maintain the availability of materials using the old links + the ability to add new materials with a user-specified link, including Cyrillic

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Shapovalov, 2016-04-14
@shapovalov_org

Apparently there is no simpler way, well, it means "and so it will do")))

M
Maxim Timofeev, 2016-04-08
@webinar

I think that all old links should be redirected to new places with an adequate structure using htaccess, otherwise you will be tormented later.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question