I have a method in my controller that handles generic page requests, meant for my public pages. I want to throw a 404 when I can't match what is in the URL and what I know are the static content pages.
So I do this:
$opts = array(
'name' => 'Some message',
'code' => 404,
'message' => 'Your message here',
'base' => $this->base
);
$this->layout = 'blank';
$this->cakeError('error', array($opts));
The problem is I get this error on a 404 request:
Notice (8): Undefined variable: javascript [APP\views\layouts\default.ctp, line 10]
The "$this->layout = 'blank';" is my attempt to redirect to a blank layout file that doens't include any CSS, JS, etc. But it's ignoring this completely and loading the default template, which has JS includes. I'm guessing that the error routines that load 404 pages don't have access to these methods, which is the source of the error.
But I can't figure out how deal with this error.
First, there's a small bug in your code. Your second parameter to cakeError() is already an array. It should be:
$this->cakeError('error', $opts);
Second, you can set
$this->layout = false;
if you want a blank layout.
Related
I have a TableObject that sends an email in the afterSave-Event to notify the admin of a change. After the switch from cakephp-3 to cakephp-4 the test for this fails with this error message:
Cake\Routing\Exception\MissingRouteException: A route matching ... could not be found.
The exception occurs on a line in the email template where I build a link like this:
$this->Url->build([
'prefix' => 'Partner',
'controller' => 'orders',
'action' => 'view',
$order->id,
]);
I believe that the routes are not set up in the context of a test for a Tableobject and therefore the reverse routing is not working. (I only get the error when running the test, not when the email is sent in the app).
Is there a way I can load all routes in the test?
Since 4.x the routes are not auto loaded anymore.
You need to add
$this->loadRoutes();
into your setUp() or before the test run to actively load them now.
I have a problem printing a PDF using CakePHP and DomPDF as soon I want to fetch some data from the Database before printing the pdf. Without fetching the Data, it works like a charme. My function is the following:
public function tourpdf($tourid = null){
$contain = ['Deliveries','Deliveries.Articletransactions','Deliveries.Orders','Deliveries.Orders.Customers',
'Deliveries.Articletransactions.Orderarticles','Deliveries.Articletransactions.Orderarticles.Articles'];
$tour = $this->Tours->get($tourid)
->contain($contain);
$this->viewBuilder()
->className('Dompdf.Pdf')
->layout('Dompdf.default')
->options(['config' => [
'filename' => $filename,
'render' => 'browser',
'size' => 'A4',
'orientation' => 'landscape'
]]);
$this->set('Test', 'Hallo');
$this->set('Tour',$tour);
}
As soon as I set the Data from the last line, that I fetched above, I get the error Message "Fatal error: Cannot redeclare class Dompdf\View\PdfView in .../src/View/PdfView.php on line 66".
However commenting the last line out, the first set with Test is working and the PDF is generated. How can I set data retrieving from a query to the pdf file?
I got a similar error, but with Zend Framework 2. All of a sudden DOMPDF would give an error "Cannot redeclare (previously declared in ...)".
I had PHP functions in some templates and when I removed them I was getting another error "No block-level parent found. Not good.". I noticed this started happening when I upgraded libxml2 from 2.9.4 to 2.9.5 or later.
The solution to both of the problems was to instantiate DOMPDF class like this:
use Dompdf\Options;
$options = new Options();
$options->set('enable_html5_parser', true);
$dompdf = new Dompdf($options);
With enable_html5_parser all the problems went away. Here is some more information on this issue.
Amazing!
Had this issue on Mac 10.12.6
PHP Version 5.6.30
libxml Version 2.9.4
Using the regex to remove whitespace between the tags fixed the issue.
$html = preg_replace('/>\s+</', '><', $html);
So basically I am trying to download a file.
I Have the action:
public function getfile() {
$this->autoRender = false;
$accesskey = 'mrPQVeJF8VFXpSq';
$data = $this->File->find('first', array('conditions' => array('File.accesskey =' => $accesskey)));
$filepath = substr($data['File']['path'], 17);
$this->response->file($filepath, array('download' => true, 'name' => $data['File']['name']));
return $this->response;
}
this throws an error : (i guess the line with $this->response->file())
Fatal Error Error: Class 'File' not found File:
C:\wamp\www\project\lib\Cake\Network\CakeResponse.php Line: 1347
The class File is an Utility of cake. So, using it as a model will probably give you trouble. I recommend you change it. Read more about reserve cake and php words here. That may be the cause of your error.
And the other cause of that error is that you are not in FilesController, and are trying to call the Find model there. for that read how to load models from other controllers (look for ClassRegistry::init or $this->loadModel().
I'm just guessing here, because your are "guessing the line giving trouble is $this->response->file()". You may get a more accurate response if you do not guess and debug exactly what line is giving you that error.
I have configured the path in the module:
function mymodule_restws_resource_info() {
return array(
'mymodule_person' => array(
'label' => t('Person'),
'class' => 'PersonResourceController',
'menu_path' => 'api/person',
),
);
}
That part seems to work, because when I send a POST transaction via Advanced Rest Client in chrome, I get a 200 response (I get a 404 if I change the request url).
That said, in the module I also throw an error if attempting an update, delete or read. When I send any of those, I still get a 200 response.
Any of GET, POST, PUT or UPDATE gives me
Response does not contain any data
including the create method in the resource controller, so it's not firing.
Do I need to do something to invoke it beyond hitting the url with a post payload?
The problem seems to revolve around the workflow_moderation module being in the mix. If I enable restws and attempt just to hit node/1.json, I get a white screen and an error in the logs. It seems to be a known issue at this point that workflow_moderation is not including the correct parms when it passes the node on. https://drupal.org/node/1238040
My Ext (Sencha) page throws this JS error when it initialises:
Uncaught TypeError: Cannot read property 'value' of null - ext-all-debug.js:89797
it errors on this line:
startUp: function () {
var me = this;
me.currentToken = me.hiddenField.value
Because 'hiddenField' is null.
Without pasting the entire page content (it's very big, and under NDA), can anyone tell what I should look for that might be on my page that's causing this?
Looks you're using the History and haven't created the appropriate hidden/iframe elements that it needs. Have a look at the history example for the version you're using.