Untraceable CakePHP Shell Error Solved At Last!

So after spending near 5 hours on Saturday looking for why my Cake Shell was not running on the online server, I finally figured it out.

The first road block I hit was with file permissions.

My web server was configured for write access to just the cache and logs folders inside of the /tmp folder. It turns out that the Cake shell needs to be able to write to the /tmp/cache/persistent. Also, the parent directory must be writeable. So I ended up changing the tmp folder with:

chmod 775 /srv/www/../app/tmp

Finally, the permissions error was gone, and then the next error came that got my searching for hours without respite.

I had written my shell app and tested it locally, ensuring I included all the needed database tables at the top of the class with: public $uses = array(…);

Everything should work now. Git push and let’s go… But no! Some new error message just right in my face.

Error: Database connection “Mysql” is missing, or could not be created.

The thing here was that I could not understand why I was getting a database error when my app was working online.

After so many hours searching on Stackoverflow, Bing, and Lighthouse I finally suspendied my frustration and decided to look at the servers logs.

Nothing in the logs made sense, I tried doing setsebool -P httpd_can_network_connect=1 to see if that would work, but that didn’t change a thing as SELINUX was already disabled.

Then I tried adding some lines with extensions config and socket path to the my.cnf file. That didn’t make any difference even after several reboots and service reloads.

Finally I remembered that I had dynamically set my connection in the database.php file used by CakePHP to choose either a default connection for localhost and a different one for the online server. Alas!!! That was it. Adding a condition after the dynamic selection fixed the problem in database.php

All I did was add this to the __construct method:

if (gethostname() == ‘Hostname‘){ //Find hostname using: $ hostname at your *nix shell
$this->default = $this->online;
}

Set debug to 2 in core.php to clear caches, and then changed it back to 0.

And that was the end of my shell nightmare.

 

Trouble with Auth Component failing to redirect to previous page

After near two hours of cycling through my code and the CakePHP API documents to find out why CakePHP was redirecting to my $this->Auth->loginRedirect instead of the $this->Auth->redirectUrl(), I finally stumbled on a similar case by someone in the CakePHP ticketing system

It turned out that there was no mention of the way the Auth Component worked in the new version of CakePHP.

I had tried getting back to the previous page I tried to access when not logged in, but CakePHP kept sending me to the default URL I specified for login redirect.

The solution I eventually found out was to ensure that I did the redirect in my login method – duh!

But that was not the only caveat. In order to redirect to the previously requested page, you must by no means make a call to $this->Auth->redirectUrl() anywhere other than when doing the redirect.

If for any reason you try to save the URL from $this->Auth->redirectUrl() in a variable and your page redirects, that variable is lost along with the content of $this->Auth->redirectUrl().

For this reason, you application will only be able to redirect you to a default URL which is what CakePHP does eventually.

Thank God I found out that the value obtained from $this->Auth->redirectUrl() is erased immediately after that method is called.

So if you are having the same issue, it might be best to call “$this->Auth->redirectUrl()”  inside your call to $this->redirect().

So you would end up with something like:

$this->redirect($this->Auth->redirectUrl());

Now that’s just a piece of cake.