cakephpcakephp-3.0cakephp-2.3cakephp-routing

How to route to a specific method in cakePHP?


my route:

Router::connect('/', array('controller' => 'users', 'action' => 'homepage'));

Router::connect('/admin', array('controller' => 'users', 'action' => 'dashboard', 'admin' => true));

my Controller

class BrandsController extends AppController {

/**
 * Components
 *
 * @var array
 */
public $components = array('Paginator', 'Session', 'Flash');


var $paginate = array('limit' => 100);

/**
 * admin_index method
 *
 * @return void
 */
public function admin_index()
{      
  index method      
}
 public function admin_searches() 
{
     searches method
}     

My link

<?php echo Router::url(array('controller' => 'brands', 'action' => 'searches')); ?>

I have a admin_searches.ctp file which has hello world text as well.

Problem is: When i have a href link as,

<a href="<?php echo Router::url(array('controller' => 'brands','action' => 'admin_index')); ?>">Brands List</a> 

it works fine and displays the index page. Now when i want to display a search page "admin_searches,ctp" it wont. How may I route in the link or amend route file so that when i call the above link it invokes search method in my controller class


Solution

  • This is a authentication issue. Everytime i try to create a new file eg: admin_search and a controller method called admin_search it echoes "you are not permitted to acess this location." Therefore, to overcome this issue i had to include authentication of a method in beforeFilter();

    public function beforeFilter() {
                parent::beforeFilter();
                $this->Auth->allow('admin_searches');
        }