Home » Php » php – Tabs being outputted with cakephp?

php – Tabs being outputted with cakephp?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’ve developed a web application with cakephp locally in Windows XP with xampp and everything worked just fine. Today I deployed it to a CentOS and an Ubuntu Server and with both I had a problem.
What’s happening is that an Tab space is being outputed together with my information. Let’s say I make an ajax call that is supposed to return OK, what’s being returned is " OK", more exactly (0x09)OK

I don’t really know where this tab is coming from.
I’ve reduced my code to the maximum and still I get it.

Controller

public function sendcomment() {
        $this->layout = 'ajax';
        $this->set('ret', 'OK');
    }

View

<?php echo $ret; ?>

Even when i don’t use the view file and output info directly on the layout, the tab is still there:

Controller

public function sendcomment() {
        $this->layout = 'testlayout';
    }

testlayout.ctp

OK

This is happening to all actions, no matter what the layout is.

Does anybody went through this and know what may be happening?
All my files are encoded in UTF-8 Without BOM and they work just fine thorough XAMPP on Windows.

–Edit:

Alright, some said it’s a config problem, I really don’t know.
However, trying to find where’s the ‘tab’ coming from, I started putting echoes on the core files and moving forward and forward until I finally get to where the tab is.
As of index.php from the cake webroot, if I place echo 'a' before the dispatch() method, my output is a(0x09)OK. So the tab is indeed coming from cake.

Now here’s the problem, looking at the results of several tests I finally got stuck and it has lead me to nothing.

Right now I’m on (CakeRoot)/lib/Cake/Routing/Dispatcher.php. Here we have the following method:

protected function _loadController($request) {
    $pluginName = $pluginPath = $controller = null;
    if (!empty($request->params['plugin'])) {
        $pluginName = $controller = Inflector::camelize($request->params['plugin']);
        $pluginPath = $pluginName . '.';
    }
    if (!empty($request->params['controller'])) {
        $controller = Inflector::camelize($request->params['controller']);
    }
    if ($pluginPath . $controller) {
        $class = $controller . 'Controller';
        App::uses('AppController', 'Controller');
        App::uses($pluginName . 'AppController', $pluginPath . 'Controller');
        App::uses($class, $pluginPath . 'Controller');
        if (class_exists($class)) {
            return $class;
        }
    }
    return false;
}

I’m exactly at this part:

    App::uses($class, $pluginPath . 'Controller');
    if (class_exists($class)) {
        return $class;
    }

Now here are the tests, if i put an echo right before the if, like this:

    App::uses($class, $pluginPath . 'Controller');
    echo 'a';
    if (class_exists($class)) {
        return $class;
    }

My output will be a(0x09)OK.
However if i put my echo right inside the if like this:

    App::uses($class, $pluginPath . 'Controller');
    if (class_exists($class)) {
        echo 'a';
        return $class;
    }

My output is gonna be (0x09)aOK.
The only thing that comes to my mind is that class_exists() is echoeing the tab. But that just doesn’t make any sense.
For testing purposes i made this:

    App::uses($class, $pluginPath . 'Controller');
    if (TRUE) {
        return $class;
    }

Still the tab is being outputed. Even worse, if I do an if == TRUE like this, no matter if the echo “a” is before the if or right inside the if, the ouput will always be a(0x09)OK.

What is going on here?

How to&Answers:

So the tab is indeed coming from cake.

This is an incorrect conclusion.

You almost have it

Paraphrasing your code:

App::uses($class, $pluginPath . 'Controller');
echo "before";
if (class_exists($class)) {
    echo 'after';
    return $class;
}

Outputs: before(0x09)after

There is no great mystery here. Used in this way, class_exists will autoload classes – i.e. include them, and any immediate class dependencies (as they will also invoke the class loading). Therefore there is a very short list of places it can come from:

  • Controller/$classController.php
  • Plugin/Foo/Controller/FooAppController.php (if it’s a plugin controller)
  • Controller/AppController.php (if it exists)

One of these files has leading or trailing whitespace – simply find it and remove it.

Use the tools at your disposal

You do not need to guess what file is the problem, there are many, many tools which will tell you which file is the culprit. One such tool is in the debug kit plugin, e.g.:

-> Console/cake DebugKit.whitespace

Welcome to CakePHP v2.3.0-RC2 Console
---------------------------------------------------------------
App : app
Path: /path/to/app/
---------------------------------------------------------------
Checking *.php in /path/to/app/
!!!contains trailing whitespaces: /app/Controller/AppController.php