How to fix "Error: NewController could not be found." error in CakePHP2 - cakephp-2.0

Can't seem to overcome the problem where I HAVE to create a NewController even though I don't want that name for the controller.
I have configured the database and that's about it.
database.php:
class PostsController extends AppController {
public $helpers = array('Html', 'Form');
public function index() {
$this->set('posts', $this->Post->find('all'));
}
}
Hey, so I'm new to CakePHP and I'm using the 2nd version. I have configured the database.php file to my requirements but as soon as I open the website path, I get an error saying "Error: NewController could not be found.".
Also, "Error: Create the class NewController below in file: app\Controller\NewController.php"
I don't want to create a NewController rather I am creating a PostsController to display posts I have in my database.
Thank you!

I finally found the answer.
The problem was just that I had created nested directories within the 'www' folder where I was saving my project.
All I had to do was place the project folder in the 'www' directory and it worked!
Thank you all. Have a great day :)

Related

cakephp 2 how to suppress or catch missing controller exception

Whenever somebody tries to access a non-existant url on my cakephp app I get a missing controller exception like this:
MissingControllerException: Uncaught exception 'MissingControllerException' with message 'Controller class AutodiscoverController could not be found.' in /app/Vendor/cakephp/cakephp/lib/Cake/Routing/Dispatcher.php:161
I guess this is as it should be, but it creates a problem with New Relic. This is our monitoring application and it will always register these exceptions and notify everybody that there is a problem with the application.
Is there a way to catch the exception or suppress it so that New Relic does not register it?
This is how I ended up solving my problem:
I installed new relic's PHP agent: sudo apt-get install newrelic-php5
Then I configured an ExceptionHandler for my app. In core.php:
Configure::write('Exception.handler', 'AppExceptionHandler::handleException');
In bootstrap.php:
App::uses('AppExceptionHandler', 'Lib');
The handler is located in app/Lib/AppExceptionHandler.php and it looks like this:
<?php
class AppExceptionHandler extends ErrorHandler{
public static function handleException($error) {
if(get_class($error) == 'MissingControllerException') {
if (extension_loaded('newrelic')) {
newrelic_ignore_transaction();
}
}
parent::handleException($error);
}
}
The handler filters through all exceptions and if a MissingControllerException comes it's way, it uses the New Relic PHP Agent to ignore the current transaction. After the filtering the normal handleException() method by cake's ErrorHandler is excecuted.
Yes you can manage it in you App controller -> beforeFilter function
if($this->name == 'CakeError'){
// Perform any action in error
}

CakePHP Globally override class?

I reconfigured my cakeEmail class to log to a specific type by rewriting the send method. I would like to use this override globally. My current single file setup uses /Lib/CustomCakeEmail.php with
App:uses('CustomCakeEmail', 'Lib');
CakePhp: Cake Email AfterSend event suggests a method to globally override using AppController but I have been unable to even trigger the debugger in
App::uses('CustomCakeEmail', 'Lib');
class AppController extends Controller {
public function getEmailInstance($config = null) {
CakeLog::write('debug', 'appcontroller triggered');
return new CustomCakeEmail($config);
}
What is the correct way to implement this global override?
CakePHP Version 2.8.4

Class not found in a CakePHP shell script

I'm trying to include some classes in my shell script in CakePHP but they don't seem to load.
For example I have a class located in
/app/Lib/php-ews/EWSType/FindItemType.php
My script looks like this :
App::uses('FindItemType', 'Lib/php-ews/EWSType');
class TestShell extends AppShell {
public function main() {
$this->out($this->readbox());
}
public function readbox() {
$request = new EWSType_FindItemType();
}
}
This gives an error:
"PHP Fatal error: Class 'EWSType_FindItemType' not found in "
Not sure what I'm doing wrong here.
PHP Exchange Web Services should be installed in the Vendor folder and loaded via App::import().
From the CakePHP 2.x docs:
Your vendor files may not follow conventions, have a class that differs from the file name or does not contain classes. You can load those files using App::import().
In your case:
App::import('Vendor', 'FindItemType', array('file' => 'php-ews/EWSType/FindItemType.php'));

Custom datasource in CakePHP not working

I’m trying to create a custom datasource for Amazon Web Services in CakePHP. My approach is as follows:
Base AwsDataSource that creates signatures, makes the actual HTTP requests etc
Various datasources for each AWS product (i.e. S3, SQS etc) that extend this class and specifies the endpoint to use
Models for things like S3Bucket, SqsQueue, SqsMessage and so on
My base datasource class looks like this (simplified):
<?php
class AwsDataSource extends DataSource {
public $config = array(
'key' => '',
'secret' => '',
'region' => ''
);
public $endpoint;
public function signRequest($parameters) {
// generates signature
}
public function makeRequest($parameters = array(), $method = 'get') {
// generates signature and makes HTTP request to AWS servers
}
}
And a sample model looks like this:
<?php
class SqsQueue extends AwsAppModel {
public $name = 'SqsQueue';
public $useTable = false;
}
My problem comes trying to then use these models/datasources in my CakePHP app.
I’ve implemented methods named create(), read(), update() and delete() in my AWS datasource as per the CakePHP cookbook, but they don‘t seem to be getting called. I know this because I’ve put die() statements in my datasource with a message, and execution is never stopped.
I’ve exhausted the cookbook, so if any one could show me how to get my models to call the CRUD methods in my datasource classes then I’d be most grateful.
My bad. Turns out my approach was flawed.
The datasource is specified in database config and is specified as AwsDataSource. Therefore, S3DataSource or SqsDataSource is never used, even though that’s where I’ve defined my CRUD methods, hence my application never exiting (because the CRUD methods aren’t defined in AwsDataSource, the actual datasource being called).
Looks like it’s back to the drawing board.

Using a third party lib with cakephp 2.0

I'm trying to implement a third party library into a cakePHP 2.0 project.
I would like to use the PHP QR Code library to create QRCodes.
I made a new folder in app/Plugin called QrCode and put the library in the Vendor folder of my new plugin.
I created a component in Controller/Component called QrGeneratorComponent with this content:
<?php
App::import('Vendor', 'phpqrcode'.DS.'qrlib');
// Component defined in 'QrCode' plugin
class QrGeneratorComponent extends Component {
public function test() {
return QRcode::png('PHP QR Code :)');
}
}
In my application I added the component public $components = array('QrCode.QrGenerator'); and tried to access my test-method: $this->QrGenerator->test();
But I always get this error:
Fatal error: Class 'QRcode' not found in C:\xampp\htdocs\cake\app\Plugin\QrCode\Controller\Component\QrGeneratorComponent.php on line 8
So, what did I wrong? Is there a better way to implement a third party library?
I think you have to prefix the vendor path with the plugin name: App::import('Vendor', 'QrCode.phpqrcode'.DS.'qrlib');

Resources