Daruma
Daruma

Reputation: 15

cakephp disable direct access to a controller

I am new to use cakephp2, i use element + requestAction to show a news block on some page of my site, like below:

news.ctp

<?php
$news = $this->requestAction('controller'=>'News','action'=>'load');

foreach($news as $itm){
    echo $itm['title];
    //...
}

NewsController.php

<?php
//...
public function load(){
    //...
    return $data;
}

It's worked well ,my problem is

how to disable direct access like: http://domain/News/load

and if it is a good way to make a contents block?

thanks!

Upvotes: 1

Views: 1119

Answers (1)

Anil kumar
Anil kumar

Reputation: 4177

In your controller you can try like this to prevent direct access.

public function load(){
    if (empty($this->request->params['requested'])) {
        $this->redirect($this->referer());
    }
    return $data;
}

If requestAction is used without caching requestAction can lead to poor performance. It is rarely appropriate to use in a controller or model. check here

For more info you can check the documentation here

Upvotes: 1

Related Questions