Various CakePHP 2.4 tips

Serving media from outside webroot with $this->response()

If you’d like to display images and videos to a user without making them accessible via the webroot (i.e. www.mysite.com/img/photos/image.jpg”) and instead wish to store them outside the app folder (i.e. /var/www/mysite.com/public_html/photos/image.jpg) you can use the  $this->response->file(ROOT . 'photos' . DS . 'image.jpg'); in a controller method (let’s name it “PhotosController::display”) and then  return $this->response();.

By just doing something like the following <?php echo $this->Html->image('/photos/display'); ?>  will display the image from the response file method. This is a simple demo but by passing an $id and then following up with maybe checking the user’s credentials you can be in control of who gets to see certain content.

Maintenance Messages

It’s quite easy to set up a maintenance message system in your application, you just need a table to store the messages (start and end date) and something to serve them, we start with something like the following for the database table.

In your AppController, make a method to check if there is a maintenance message and include it in your beforeFilter

And then within the model for Maintenance.php (I’ll assume you know how to set that up), you want a method like the following. I use a cache for this which I setup previously, you can read more about that in the cookbook.

And that’s it, in your layout or within an element you can simple check if the variable $maintenanceMessage is isset, and if so then display the message, otherwise there is no message currently. There is plenty of room for improvement and change for a better system, this is just a simple example, just add a new record with a start and end date to the table.

Redirects & Routing (301)

Sometimes you want to route some URL’s to specific actions and views, this is especially true when you remake an old website and past url’s are indexed in Google. You could do 301 redirects from within .htaccess, but I imagine a lot of people would be tempted to use routes.php and do it all from within CakePHP. This is fine however you want to make sure that you are providing a redirect using 301 as to not have duplicate content. Here’s how to do it

Router::redirect('/some-old-url/', 'http://www.mysite.com/new-url', array('status' => 301));

You may also serve up redirects from within your controllers with a 301 status like such

return $this->redirect(array('controller' => 'foo', 'action' => 'bar'), 301);

I would always recommend using return with any controller redirect as it could cause issues with unit testing if you don’t.

Loading Components within a Shell

This isn’t something you should want to do often, I’d advise against it if at all possible but sometimes you just have to do it. Here’s how I suggest you make a component accessible within a shell, first we start by telling the App class where to look for the files we intend to load (above the class)

Secondly I would create some properties within your shell to replicate how you would expect a component to load, in the following example I’m going to be loading a “Mailchimp” component.

And finally we will load a controller object into the initialize method, we then specify that controller’s components and tie it to the properties we just created.

Now you’re free to use $this->Twitter->someAction(); within your shell as you would within a controller.

Logging & Outputting from shells at the same time

I like to keep a log of everything that goes on with my shells, it may sometimes mean sifting through a lot of data but it’s good to keep a reecord. Here’s a convenient method I use to do this easily

 

Leave a Reply

Your email address will not be published. Required fields are marked *