Back 1 2 3

SilverStripe 500 Internal Server Issue

So, if you've ever been working locally in MAMP or WAMP and everything's going smoothly, but then you upload your site to your server and BAM! 500 Internal Server Error. That could be ANYTHING! Here are a few places you should check.

  1. .htaccess
    Make sure you copied/uploaded it for one... but also make sure it has the proper permissions
  2. Inside the .htaccess!
    Just in case you had any magic stuff going on locally, but here's the one that always gets me...
    Some servers require you to add "RewriteBase /" - just add it at the top of your .htaccess file.
  3. Database connection
    Make sure you don't still have it pointing to your local mysql :)

 

How To: SilverStripe Controllers made easy

If you're like me, sometimes you just want to build out a quick action sort of link. You know, like, dropthenerd.com/action/update/$recordID, or dropthenerd.com/post/new. At first I found myself just tacking these on the Page_Controller, and spitting out links like dropthenerd.com/home/myaction/ID/OtherID... but I'm pretty specific in how I like my link structures to look - so I adopted the following approach.

_config.php:

Director::addRules(50, array(
    'action'    => 'Action_Controller',
));

mysite/code/Action_Controller.php

class Action_Controller extends Controller {
static $allowed_actions = array('post');
  
function index(){
       // do your worst
    }

function post(){
// post it
}

}

That's about it - I know, it's pretty simplistic, but you can extend this to whatever you want. Don't forget to add, your action to your $allowed_actions if it's not the index. My favorite part about this approach is the flexibility to keep very specific actions in very specific controllers - and never having to worry about a cumbersom Page_Controller - also, when you just tuck these into Page_controller, if you're also setting up css/javascript includes, then it's loading a whole lot more than just directing you to the one controller you need. It's significantly faster. Just make sure you're not extending Page_Controller.

Learning PHP and web development

So, you want to learn how to build websites using php? It's a topic that's come up in several of my circles in the past few months. One fellow developer wanting to broaden his skill set, another entrepreneur that's just not satisfied letting someone else build his project out (and rightfully so) and another college age individual that just wasn't sure what they want to do "when they grow up."  "You should", is what I tell them, and it's what I'll tell you too. You should learn php. You should learn css. You should learn to build a basic html structure page. You should familiarize yourself with xml. You should learn about mysql databases and the beauty and danger of javascript.

Everyone should learn at least the basics of what powers a website. The reason I feel so strongly about this is simple, it's the same reason you should know how to change a tire. We all use the internet these days, so it would be very helpful for you to at least know what's driving the sites you're interacting with on a daily basis. Most people don't know the difference between client side and server side, and it probably doesn't matter to them - but they are also the ones that will think Youtube is broken rather than realize that they just need to stop using Internet Explorer or update their flash plugin.

This really wasn't meant to be a rant, just my bit of encouragement and reasoning for advising everyone I know to at least go try out a "Hello World" tutorial. The absolute worst thing that could happen is that you might surprise yourself and learn something, or not... then you're right where you started.

How to create a RSS feed with SilverStripe

The greatest thing about creating RSS feeds with SilverStripe is that you can pretty much use any of your data, since all you really need is a DataObjectSet.

Here's my 2 second sample of an RSS feed:

function rss(){
        $articles = SiteTree::get('Page',"Status = 'Published'",'Created DESC');
        $rss = new RSSFeed($articles, '/rss', "Drop The Nerd :: RSS Feed", "", "Title", "Content", "DropTheNerd");
        return $rss->outputToBrowser();
    }

You can, of course, setup as many RSS feeds as you'd like. Just remember to add in an Auto Discovery meta tag:

<link rel=”alternate” type=”application/rss+xml” title=”Drop The Nerd RSS Feed” href=”http://dropthenerd.com/rss” />

Close a fancybox when submitting a form

Today I was presented with something rather strange. A client wanted a delayed lightbox presenting their user with an incentive to register on their site. The user would be given 3 available options; Never, Later, or Sign Up Now. The lightbox needed to contain a form and “Sign Up Now” would submit the data… but the other two would both close the lightbox, one setting a cookie for 90 days and the other setting a cookie for 1 day. La la la, set everything up like usual… but wait, how do I get these stinking buttons to close the form? (I’m using fancybox and jQuery by the way)

 

Here’s my solution:

function fancybox_close(){
    $('#fancy_outer').hide();
    $('#fancy_overlay').hide();
    $('#fancy_title').hide();
    $('#fancy_loading').hide();
    $('#fancy_ajax').remove();
}

If you’ll notice in the source of your page fancybox loads your desired content (if using the ajax method) into the div #fancy_ajax. To ensure the form is not just hidden, we call $(‘#fancy_ajax’).remove(); The others just hide the fancybox, which enables it to be reused for any other instances on your page. Then, simply call this function when your buttons are clicked, as so:

$('.close_popup_never').live('click',function(){
    fancybox_close();  //closes the fancybox
    return false;
}

That’s it! It took a little figuring out, but it seems to do the trick.

Note: I’ve revisited this and settled on a new approach that plays well with the newer versions of Fancybox. Check it out here: Close Fancybox when you submit a form (revisited)

Fun with Fancyboxes as custom lightboxes

Fancybox. Here we go again – but no matter how many other lightbox solutions I try, I always go back to the Fancybox. Why? Because it’s simple. It works in almost all applications where I need it t0 – and it offers everything you could possible need or want out of a fancybox. But wait, you don’t like the default styling of the fancybox? No problem, with a little creativity you should have no problem at all customizing the look and feel of your fancybox implementation.

Start by creating your own close button nestled inside your fancybox (you could just as easily override the close button’s styles, but this should also work for helping you setup multiple close button solutions).

.custom_fancybox_close {
    width: 22px;
    height: 22px;
    position: absolute;
    right: 10px;
    top: 10px;
    cursor: pointer;
    background: url(custom_fancybox_close.png) 0 0 no-repeat;
}

.some_container_class {
    background: #333;
}

Just grab a close button icon of your choice make sure you update the css (or resize/rename/reformat the graphic) and you’re all set to add the following html into your fancybox content:

<div></div>

Piece of cake.

Just a bit of jquery now to make sure your button always works:

    $('.custom_fancybox_close').live('click',function(){
        $.fancybox.close();
        return false;
    });

Side note: I like to use live to attach the click event just because you never know when you’ll be adding content dynamically and it helps you catch elements that aren’t caught by the regular .bind().

Oh yeah – here’s what my fancybox call looks like:

        $.fancybox({
            href: this.href,
            centerOnScroll: true,
            hideOnOverlayClick : false,
            showCloseButton : false,
            scrolling : false,
            padding: 0,
            margin: 0
         });

Here we go – this would be ideal for an alert style modal where you want to ensure that you get some user attention – disable click on the opaque backbground – disable the default close button (since we’re adding our own!) get rid of hideous scrolling (I like to add my own when applicable – but in this case we’re controlling the height of our fancybox) so we’re also using centerOnScroll to elegantly keep the fancybox vertically centered.

So there you have it. As long as you’re loading something similar to the following html, that’s it!

<div>
<div><!-- this is my custom close button --></div>
<div style="height: 150px; width: 300px; padding: 20px;">
<!-- just using inline styles to show you how we're controlling the height/width -->
Hey everyone!
</div>
</div>

View the working custom fancybox example here!

No, before you go assuming that I don’t like the default fancybox style – please understand that I like it just fine. It’s just that sometimes you need variety – and that’s all I’m providing here.

If you’ve got a creative Fancybox useage please feel free to share it by leaving a comment below! Thanks and happy coding!

Remove .svn files from directory recursively

Here’s another useful tip if you svn and often times copy entire directories from one project to another. You get all of those nasty little .svn files. Just open terminal, type cd and drag the directory in (after having copied it to your desktop) hit return, and run the following command. This will remove all .svn files recursively from directory you’re in.

find . -name ".svn" -type d -exec rm -rf {} \;

Hope it helps! Enjoy!

Custom FancyBox close button or link

Creating a custom close button for your FancyBox is really pretty simple. Sometimes we use the FancyBox and just need to style it differently,  or to not display the default close button and add our own – or we just want to use it for notifications/alerts and provide a “Close” button of our own after our message. You can accomplish all of the above with the fallowing little bit of javascript.

$('.Custom_FancyBox_Close').live('click',function(){
    $.fancybox.close();
    return false;
});

Now you can add all of the pretty little close buttons inside your FancyBox – but just remember if you’re loading in iframes you may have to include this javascript within the loaded content, but utilizing the ‘live’ method should take care of content loaded via ajax or just hidden divs. Hope you enjoy, and happy coding!

Quick SVN Tips for ignore and externals

We work with SVN every day, but not every aspect of it. Some of the more commonly forgotten SVN features I always have to look up are ignore and externals – although they’re both pretty much the same to use. Here’s my preferred approach though – it enables you to use your favorite editor (vi, nano, etc etc etc). I prefer nano, but I’ve listed vi as well below.

Set your editor:

export SVN_EDITOR=nano
or
export SVN_EDITOR=vi

Move to the correct directory:

svn propedit svn:externals .
or
svn propedit svn:ignore .

The next part is easy, just enter the name of the local directory followed by the externals path, like so:

externals-folder http://myothersite.com/my-other-project/externals-folder/

And run svn up.

Or, if you’re doing svn ignore, same thing, but just the name of the folder or files to ignore.

file_to_ignore.ext
folder-to-ignore

Again, just run svn up.

Don’t forget though – these are not recursive, so if the directory you want to ignore is in /public/web/, then make sure to cd /public/web/ first. I hope that makes sense.

Alias local paths to remote images

Don’t you hate it when your local development environments just show you a bunch of broken images? This typically happens with sites hosting photo galleries, lots of product shots, etc. You don’t necessarily want to have all of your images on your local, but of course they need to be there on your live server, and rather than write out absolute paths this little trick will come in handy.

In your htaccess file just add the following:

RewriteEngine On
RewriteRule ^(product_images/.*) http://www.example.com/$1 [L]

So there you have it, hope it helps and have fun coding!

Back 1 2 3