phpcakephp-2.0cakephp-routing

Link with another prefix on a Previously prefixed page in cakephp:2.5.4


I am trying to achieve the link as cake_proj/prefix2/controller2/action on this page who's link is - cake-proj/ but 'prefix1' => true in my routes.php as this is the home page, so ultimately its link is cake_proj/prefix1/controller1/action. So now I am trying to achieve cake_proj/prefix2/controller2/action with the help of Html helper's link method-

<?php
  echo $this->Html->link(
    '<i class="glyphicon glyphicon-group"></i> Test Link', array(
      'prefix' => 'prefix2',
      'controller' => 'controller2',
      'action' => 'prefix_action'), array(
      'escape' => FALSE)
   );
 ?>

But with this I am getting the link as cake_proj/prefix1/controller2/prefix_action notice here it doesn't change the prefix. I don't want to loose the CakePHP's routing capability, but still want to get over this problem. I referred some of the previously answered questions but not worked for me. Please help.


Solution

  • A solution for you.. though not tested works perfectly for me. Here is the solution, cheers!

    in AppController.php

    public function beforeFilter() {
            $this->set('prefixUsed',$this->request->prefix);
    }
    

    in your_view.ctp

    <?php
      echo $this->Html->link(
        '<i class="glyphicon glyphicon-group"></i> Test Link', array(
          $prefixUsed => false,
          'prefix2' => true
          'controller' => 'controller2',
          'action' => 'prefix_action'), array(
          'escape' => FALSE)
       );
     ?>
    

    Here the idea is to set a variable $prefixUsed in AppController.php to be used by each and every views and even by layouts in beforeFilter(), and then using that variable into views to set it to false and making your desired 'prefix' => true. We are using $prefixUsed to dynamically determine the prefix. Let me know if this worked or not for you, till then GoodBye... ;)