cakephp REST problem - cakephp

** SOLUTION **
I am using cakephp 1.3. I did not have to add any routes. All i did is after adding the action to the controller, I went to the url
/controller/action/id
in my case it was
/test/bravo/123
and that did the trick
** End **
I am using cakephp 1.3 and I am trying to add webservice. I heard its very easy to add restful. What I want to do is to have one of the actions in my controller provoked and log into a logfile as a success. Lets say
function bravo(){ $this->log("success", "Testlog");
So far I went through documentation and added the following to my routes.php
Router::mapResources('tests');
Router::parseExtensions();
then in test controller I have this
<?php
class TestsController extends AppController {
var $name = 'Tests';
var $components = array('RequestHandler');
.....
.....
function bravo(){
$this->log("success", "Testlog");
}
?>
Now when I go to
/tests.bravo
, its not logging the success in the log file. What am I doing wrong. Thanks

Related

Cache View in CakePHP 3.0

I understand that we can cache an action or view page in CakePHP 2.0. Refer to this link http://book.cakephp.org/2.0/en/core-libraries/helpers/cache.html.
class PostsController extends AppController {
public $helpers = array('Cache');
}
public $cacheAction = array(
'view' => 36000,
'index' => 48000
);
However, it seems that CakePHP 3.0 has removed helper(http://book.cakephp.org/3.0/en/core-libraries/caching.html). Is there any other way in CakePHP 3.0 I can cache the view page. For example, I have a index view/action. And I would like to cache that page. Thanks.
Is there any other way in CakePHP 3.0 I can cache the view page.
Not without developing something yourself. Like for example saving the rendered content to a file in callback for Dispatcher.afterDispatch event and then checking for the file in callback for Dispatcher.beforeDispatch event and returning the cached response.
Better to use you something like Varnish which is more suited for the job.

CakePHP - Class 'AuthComponent' not found in cached view

I'm using CakePHP 2.3. and I'm trying to cache my home page view. But After caching
it to home.php, I can't load cached view because of error:
Error: Class 'AuthComponent' not found
File: C:\wamp\www\project\trunk\app\tmp\cache\views\home.php
Line: 87
I use AuthComponent for realizing if user is logged in or not. Without caching Everything works.
Controller code:
public $helpers = array('Cache');
public $cacheAction = array(
'home' => '60 minutes',
);
Thanks
App::uses() your Auth component in your bootstrap would help:
App::uses('AuthComponent', 'Controller/Component');
This way Cake knows where to load the class from if its needed (even in cache mode).
That would be this line in the file \config\bootstrap.php
App::uses('AuthComponent', 'Controller/Component');
Note that I tried this in Cake 4.1.5 but it tlls me "Class 'AuthComponent' not found"

Extends cakephp plugin

I'm using spark_plug plugin on cakephp, this plugin provides an authentication-acl system for register and admin users in cakephp. I want to add some new code and functionalities to the user's controller but I don't want to change the "main" plugin files.
I was thinking if it is possible leave the "main" plugin controller as it (unchanged) "\app\plugins\spark_plug\controllers\users_controller.php" and create a secondary controller with all the new code and functionalities, something like this "\app\controllers\users_controller.php" and extends the plugin "main" controller.
Is that possible? and how achieve that?
Or do you think is there any other way to do what I want?
Thanks!
You could perhaps use composition rather than inheritance? I.e. create a "app\controllers\users_controller" that has inside it an instance of the plugin's controller. The UsersController passes through any unmodified actions via stubs, eg:
class UsersController extends AppController {
...
var spark_plug_users_controller;
...
public function __construct() {
parent::__construct();
App::import('Controller', 'SparkPlug/Users'); // this is probably wrong.
$this->spark_plug_users_controller = new UsersController; // as is this.
$this->spark_plug_users_controller->constructClasses();
}
...
//example non-overridden method
function login() {
return $this->spark_plug_users_controller->login();
}
...
}
your problem would be accessing protected/private methods within the spark_plug Users controller. But if you did not need to, this may work.

cakePHP : x_with in action / function causes errors... repeatable?

In a controller, make a dummy action, name it like stackoverflow_with_something()//the WITH part is important
Create the view stackoverflow_with_something
Load up the page in a browser. Get a 'missing view error'
Take out the _with, try again. No error.
I havent seen any documentation on this, whats the deal?
short version : Why does xxxxx_with_xxxxx crash cakePHP, but xxxxx_xxxxx dosent?
No problems here, tried with the following:
/app/controllers/foos_controller.php:
<?php
class FoosController extends AppController {
var $uses = null;
function bar_with_yadda() {
}
}
?>
/app/views/foos/bar_with_yadda.ctp:
Hello
requesting /foos/bar_with_yadda returns no error.

ControllerFile not found error in cakephp

HI! I'm trying to create the web service in the cakePhp. I'm new to cakePhp and only recently start working on it. I found a useful tutorial at http://www.littlehart.net/atthekeyboard/2007/03/13/how-easy-are-web-services-in-cakephp-12-really-easy/
I created both the controller and index.ctp files as described in the tutorial. But when I typed the url (http://localhost:81/cakephp/foo) of the controller to run the file, I got the following error:
// controllers/recipes_controller.php
/**
* Test controller for built-in web services in Cake 1.2.x.x
*
* #author Chris Hartjes
*
*/
class FooController extends AppController {
var $components = array('RequestHandler');
var $uses = '';
var $helpers = array('Text', 'Xml');
function index() {
$message = 'Testing';
$this->set('message', $message);
$this->RequestHandler->respondAs('xml');
$this->viewPath .= '/xml';
$this->layoutPath = 'xml';
}
}
CakePHP: the rapid development php framework
Missing Controller
Error: FooController could not be found.
Error: Create the class FooController below in file: app\controllers\foo_controller.php
Strange thing is that (everyone can see) that controller text is loaded in the error page, but error shows that controller file is not found.
I also tried to follow the tutorial on book.cakephp.org/view/477/The-Simple-Setup.
But same error also occured here. Anyone can help? By the way I also changed the text of routes.php to work it with web webservices.
Thanks
The fact that the contents of your FooController file is being output in the browser indicates that the PHP is not being executed.
You need to ensure that the definition for your FooController class is enclosed in <?php and ?> tags, like this:
// controllers/recipes_controller.php
/**
* Test controller for built-in web services in Cake 1.2.x.x
*
* #author Chris Hartjes
*
*/
<?php
class FooController extends AppController {
var $components = array('RequestHandler');
var $uses = '';
var $helpers = array('Text', 'Xml');
function index() {
$message = 'Testing';
$this->set('message', $message);
$this->RequestHandler->respondAs('xml');
$this->viewPath .= '/xml';
$this->layoutPath = 'xml';
}
}
?>
You have entered the URL http://localhost:81/cakephp/foo. Cake correctly interprets this to mean you are looking for the index action on the FooController. The error doesn't mean it has found the file, just that it has worked out what to look for but hasn't found it where it expects it to be.
The line: Error: Create the class FooController below in file: app\controllers\foo_controller.php tells you what should be there (and what, as a minimum, it should look like). Check that you have named the file correctly and that it located where the error says it should be.

Resources