cakephp Supress All Headers And ALL data - cakephp

I am trying to create a subscribable web cal. The file works, if I link to it directly. But when generated out of CAKEPHP, even with a blank layout, the calendar program says the data is invalid. I am guessing there are some hidden headers, data, something that cakephp is sending in the background. Any way to have cakephp just send the actual file?
Any other ideas why I can't subscribe?

Make sure that you put Configure::Write('debug',0); in the action in your controller that returns the data for the calendar. And then make sure you call a layout that ONLY has <?php echo $content_for_layout; ?> and nothing else in it using $this->layout = ‘yourlayout’; in that same action in the controller.

Related

Which file will be loaded first in cakephp?

I am working with cakephp , i need some clarification regarding initiating.
which file will be loaded first in cakephp whether index.php or bootstrap file
The answer of Ganesh is not really correct. It does not redirect and it does not load AppController as third file, nor does it load it ever directly.
First you should always configure your site to be accessed from the app/webroot folder NOT the index.php in the level above, because if you do that you expose the whole app structure to the public web as well.
When app/webroot/index.php is accessed CakePHP sets a bunch of constants like the CAKE_CORE_INCLUDE_PATH, WWW_ROOT and a few others, the best is to have a look at this file.
Then it will include the bootstrap.php file.
At the end of this file you'll see that not AppController but the Dispatcher is called first and the Request/Response classes are passed to it.
$Dispatcher = new Dispatcher();
$Dispatcher->dispatch(
new CakeRequest(),
new CakeResponse()
);
See Dispatcher::dispatch().
Then, still no controller is loaded. It first fires events and by this dispatcher filters which can interrupt the request and already send data back to the client. That's how the AssetDispatcher works for example. Again, still no controller here.
If the filters passed then the dispatcher will call the controller that matches the requested url, not AppController, if you called /users/index it will instantiate the UsersController and call its index() method. See Dispatcher::_loadController().
All your Controllers should extend AppController but AppController actually never gets called directly.
When a cakephp application is accessed first it will load the index.php from there it redirects to bootstrap.php and then AppController.php.

Security Component and PostLinks

Using CakePHP 2.3. I turned on the security component, and I noticed an odd behavior with postLinks, where for some reason they are no longer seem to be transmitting data by POST, but instead they are using GET. In the action I'm trying to call, the first thing I do is to ensure that the data was made by POST:
if (!$this->request->is('post'))
{
throw new MethodNotAllowedException();
}
When the security component is on, this if statement is false. When it is off, the if statement is true. No other changes have been made.
The postLink:
<?php echo $this->Form->postLink($this->Html->image('icons/resend-icon.png'), array('action' => 'resend', $invoice['Invoice']['number']), array('escape' => false, 'class' => 'hastip', 'title' => 'Resend'), __('Are you sure you want to resend this invoice?')); ?>
As far as I've been able to look, I've found no explanation for this. I would prefer if I could make sure the data is actually being sent by POST, though everything else works if I remove the check for the request is a POST.
Edit:
I've discovered that if I set $this->Security->csrfCheck = false; and $this->Security->validatePost = false; in the before filter for that particular action, it does not have this problem. I would still like to know why precisely this is though.
Edit 2:
After more investigation, I discovered the view of the page in question has another form on it, echo $this->Form->create('Invoice', array('type' => 'get')); which should not be affecting the post links in any way (post links are not inside the form, etc.), but if I remove the array('type' => 'get'), the postLinks start working. I need the other form to be of type get though, as it's a search form, and I need to have the search query string in the URL.
Edit 3:
I've discovered that moving the search form below the post links also fixes the problem. I tried running the markup through a html validator to make sure nothing was malformed, but it did not report anything.
Edit 4:
I discovered that the markup being generated for the PostLinks is incorrect -- the hidden inputs used for detecting CSRF are named incorrectly, resulting in it failing CSRF tests. Thus, the request is getting blackholed. I have set up a blackhole callback to redirect http:// to https://, so the page gets redirected, resulting in a new get request for the same page, which then gets rejected by the MethodNotAllowedException. Trying to investigate now why the PostLinks aren't being generated correctly.
I determined the solution is after finishing with the first form, I needed to reset the Form helper's request type.
<?php echo $this->Form->create('Invoice', array('type' => 'get')); ?>
<?php echo $this->Form->end(); ?>
<?php $this->Form->requestType = null; ?>
$this->Form->requestType starts as null, and gets set by calling $this->Form->create. So if I set the earlier Form to be post, or if I had created the Form after using postLinks, the Form helper's request type is set to a value that will work with PostLinks when I attempted to create the postLinks, but it appears manually resetting requestType also works.
Edit: This is actually a bug with the CakePHP framework. I have reported it, and it has been fixed, so this is no longer necessary in the newest version of CakePHP.

CakePHP is unwantedly encoding HTML entities in View output via ExceptionRenderer

I'm attempting to create a custom 404 page in CakePHP 2.0 by extending ExceptionRenderer. Everything is working fine except when I output HTML marked-up strings in the View Cake is unwantedly encoding the HTML entities. How can I prevent this from happening?
In my renderer I have:-
class AppExceptionRenderer extends ExceptionRenderer {
public function missingController($error) {
$this->controller->set('test', '<p>Test</p>');
header('HTTP/1.1 404 Not Found');
$this->controller->render('/Errors/error404', 'default');
$this->controller->set('title_for_layout', 'Page Not Found');
$this->controller->response->send();
}
}
In the view (View/Error/error404.ctp):-
<?php echo $test ?>
This outputs <test> rather than <p>test</p>.
In my actual code test will be being set by content from the database as this is a CMS driven site. I'm just setting test in the renderer code above as an example (and to prove the code is behaving as I am observing).
You shouldn't be using HTML inside your PHP, you should write any markup in the view file.
I've not come across a situation when you would need to write the markup inside your PHP code, but if you have no other way around it you could always use the html_entity_decode() function inside your view.
Unfortunately I don't know a way to stop Cake from encoding it automatically like you want.

render a blank view in cakephp

i need to prevent a view to be rendered in a specified case but i can't understand how to prevent it to render.
I tried
$this->autoRender=false
but nothing happened, probably because i'm using an API engine that manage rendering differently from regular controllers. Anyone know any trick to do this?
Using $this->layout = 'ajax' does not seem to be enough.
But using these both lines works:
$this->layout = 'ajax';
$this->render(false);
While searching for a solution, I found this answer. Now when using CakePHP 2.4.x, you could use the following code in your controller:
$this->layout = false;
This will lead to just the view being rendered, without a layout.
It's an old question. The current cake-version is 3.x and there is a easy way to use a blank layout.
Only add the in the controller:
$this->viewBuilder()->autoLayout(false);
Try to use ajax layout $this->layout = 'ajax' this is the default empty layout, which is used for ajax methods.
public function function_without_layout(){
$this->viewBuilder()->autoLayout(false);
echo "hello Brij";
exit;
}
$this->layout = false; is deprecated in CakePHP version 3.
Use $this->viewBuilder()->autoLayout(false); for CakePHP version 3.
Add this in your controller:
$this->autoRender = false;
This works in my project.
The CakePHP 3 autoLayout(false) method from the other answer will still have the system try to locate a corresponding view/template file for the action you're calling. Since I needed no output at all, this didn't work for me, so I needed to also render an empty template.
Creating a blank .ctp file for every empty action you might need isn't an option really, because you'd normally want to have one and reuse it. CakePHP 2 had a $this->viewPath property which would let you configure the controller to look into the app/View folder, but it's CakePHP 3 alternative still looks into the corresponding controller and prefix folders. There is a not-so-obvious way to force CakePHP3 to look for a template in a root view path.
Create src/Template/my_blank_view.ctp
Add the following to your controller action:
$this->viewBuilder()->layout(false);
$this->viewBuilder()->templatePath('.'); // this
$this->viewBuilder()->template('my_blank_view');
Also, I'm using $this->viewBuilder()->layout(false) instead of autoLayout(false) because the latter kind of implies that there might be another layout set later, where the layout(false) just explicitly sets that there's no layout needed.
without knowing anything about API engine you're using, maybe try to make empty layout with empty content and call it in controller as $this->layout = 'empty_layout'

CakePHP Redirect to External URL

In CakePHP, I want to create a custom URL that points from my site to another site.
Example: example.com/google would redirect to http://www.google.com
I'm a self-taught CakePHP newcomer and just can't figure out the steps. From my homework, I think I can create a route to a controller/action in config/routes.php, but I don't the right terminology to create the action in the controller.
If you want to redirect directly form controller to an external url the we can directly use
$this->redirect('http://www.google.com');
from our controller. It will redirect you to the mentioned address. This works perfectly fine.
You don't want a "redirect", you want to create a hyperlink.
Use Cake's built-in Html helper.
In your controller...
var $helpers = array( 'Html' );
In your view...
echo $this->Html->link( 'Google link!', 'http://www.google.com/' );
A "redirect" is commonly used to refer to redirecting the script on the server side. For example, after a user fills out a Contact form you may want to email yourself the details and then redirect the user to a "Success!" page with the following controller code
$this->redirect( '/contact/success' );
Using CakePHP HTML helper is your best bet.
echo $this->Html->link('Link Text Here', 'http://www.anywebsiteyouwant.com);
If it's simple enough, you could just use straight HTML.
What you need is something like:
Router::redirect('/posts/*', 'http://google.com', array('status' => 302));
This would redirect /posts/* to http://google.com with a HTTP status of 302.
See http://book.cakephp.org/2.0/en/development/routing.html

Resources