Tuesday, January 13, 2015

Re: CakePHP 3: 2 Qs - Response From Filter & Helper Constructor

You are setting this message:
$response->body(json_encode([ 'errors' => [ 'Invalid request to api' ] ]));

But getting this error:

{"errors":["Invalid request to inventory_api"]}

That makes me think that you are setting the response from somewhere else, and not where you think.

In your custom helper you would do:

 public function __construct(\MyNS\View\AppView $view, $config=[]) {

And that should fix it!

On Tuesday, January 13, 2015 at 3:45:41 PM UTC+1, Chris White wrote:
Hi all,

I'm porting a fairly sizable CakePHP 1.3 app to CakePHP 3 and have run into a couple of issues with which hopefully someone can help me out.

Rejecting a Request from a Filter

First, I'm trying to reject a request from a filter. I am setting the response's type to json and its status code to 403, but the app is still reporting 200. The code is:

$response = $event->data['response'];            
$response
->type('application/json');
$response
->body(json_encode([ 'errors' => [ 'Invalid request to api' ] ]));
$response
->statusCode(403);

$event
->stopPropagation();
           
return $response;

Request, response, and status are:

Status 200 OK

Request headers

Accept: application/json
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Cookie: CAKEPHP=717f0482276fca1b0f3c3c7155923657

Response headers

Date: Mon, 12 Jan 2015 23:30:33 GMT
Server: Apache/2.2.29 (Unix) mod_fastcgi/2.4.6 mod_wsgi/3.4 Python/2.7.8 PHP/5.6.2 mod_ssl/2.2.29 OpenSSL/0.9.8za DAV/2 mod_perl/2.0.8 Perl/v5.20.0
X
-Powered-By: PHP/5.6.2
Content-Length: 48
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8 Response

{"errors":["Invalid request to inventory_api"]}


I'm guessing I'm somehow doing this wrong?

Constructing a Helper

Second, I have several helpers but they are failing to be constructed due to type issues. While I have been using PHP off and on for nearly 15 years (since version 3),  I am new to the namespacing found in the newest versions, so it may be related. Below is one example. I am adding to the helpers collection thusly in the AppController:

$this->helpers[] = 'NavBar';



The class constructor is defined as:

namespace MyNS\View\Helper;

use Cake\View\Helper;
use Cake\Core\Configure;

class MyHelper extends Helper {
   
   
public function __construct(\MyNS\View $view, $config=[]) {



The warning I am receiving is:

Argument 1 passed to MyNS\View\Helper\MyHelper::__construct() must be an instance of MyNS\View, instance of MyNS\View\AppView given


Thanks for any help you may be able to provide to either question!

Chris

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: