Home

Using SilverStripe URL Parameters to display DataObjects on a page

Requirements

v2.2 +

Attached Files


Completed Files(1.7 KB)

Many of you may have come across the SilverStripe URL parameters in which take the form $Action/$ID/$OtherID. These are very useful for creating pages that act as templates for a DataObject. Lets say you had a Number of staff that you wanted to enter into the CMS as DataObjects on a StaffPage page type. This would mean that staff members would not have their own page on the site. In an ideal world the StaffPage itself would be able to display a list of all staff member as well as each one individually, based on a name passed as a parameter, for example www.yoursite.com/staff/jonny-cache.

Well in this tutorial we are going to create a system just like this, the only difference is that in order to keep things simple we will be using the staff members ID as the parameter, so the URL would look like this:  www.yoursite.com/staff/15. The reason for doing it this way is that we won't have to worry about creating a URL field for each staff member, making sure it doesn't contain special chars and keeping it unique, which would form a tutorial in it self. By suing the ID we can be sure of all of this without having to do anything! On the downside it doesn't make pretty URLs and it's not SEO friendly for the same reason. Well it is only a tutorial after all!

 

Part 1: Creating the StaffMember DataObject

The first thing we need to do is create the StaffMember it self. We are going to keep this simple and just have a photo, their name and a short description. Obviously you can make this as detailed as you like:

mysite/code/StaffMember.php

This should be pretty familiar to you, just a sandard DataObject class. The only thing that might be new is the getThumb() function. This is used to return a thumbnail of the photo to the DataObjectManager so that you get a nice preview of the person in the table.

Part 2: Creating our StaffPage

Now we need to create the StaffPage that is going to hold all our StaffMember DataObjects. Again this is pretty standard stuff, we create a has_many relationship and define a DataObjectManger as the fieldtype to manage it:

mysite/code/StaffPage.php

You could also use a complexTableField if you are mad want to. Notice that in the field label definition we call Thumb not getThumb. This is making use of PHP's magic Methods  which allows you to add get to the start of a function, without having to use it when calling from a template.

Now lets also just create a basic template for our staff page that will display a list of all the StaffMembers photos:

themes/blackcandy/templates/Layout/StaffPage.ss

Part 3: The funky function

So we have our classes and a basic template which is displaying our staff members. Now what we need to do is create a function which checks to see if there is a URL parameter set and if there is checks the DataBase for a StaffMember with that ID, which if found would then be returned to the template to render. So here is the function which is placed in the StaffPage_Controller class:

mysite/code/StaffPage.php

So lets go though this function. The first line gets the URL parameter 'Action' (i.e. the first parameter) and tries to assign it to the variable $URLAction.If it fails we know that there is no action and we can return to the template. Next if there was an 'Action' defined then we 'sanitize' the argument to ensure there is no malicious code in there that could harm the database (if you are wondering why you need to sanitize, read this).

Finally we check that the argument is numeric before we pass it to DataObject::get_by_id() which will try to fetch our staff DataObject. If the ID is wrong, then it will just return false, which we can then use in our template to test whether to render an individual staff member or the list.

Part 4: Displaying it all in the template

So now lets take a look at our template and see how we can use this function to decide what to display:

themes/blackcandy/templates/Layout/StaffPage.ss

The function we created in the last step will return false either if there is no action parameter defined or if the defined action is not the ID of a StaffMember. So we first test whether there is an IndividualStaffMember to display which if there is we display some detailed info about that staff member, otherwise we simply display the list of staff all members, this time with a link to view that staff member in more detail. To construct the link we simple join the MenuPage's Link (by using $Top.Link which will jump out of the control loop and get the Link from the main page we are on) with the ID of the staff member in our control loop, giving us URLs like "staff/3". We also wrap these variables in {} to make sure SilverStripe reads them properly.

So now we have a page which will display our dataobjects individually as well as in a list. The last thing we need to do is make our sidebar read them too.

Part5: Creating the Sidebar

To display the DataObjects in the sidebar we need to replace our standard sidebar with a custom one:

themes/blackcandy/templates/Includes/StaffSidebar.ss

So as you can see this looks very much like the standard blackcandy sidebar, the only difference being that we have replaced <% control Children %> with <% control Staffmembers %> which will get all our staff members and create links for each. Again we use the {$Top.Link}{$ID} to generate the link. One thing you might notice is that because we are using a single page for all of these, the $LinkingMode will no longer work. So to remedy this lets create our own function LinkingMode() inside the StaffMember DataObject:

mysite/code/StaffMember.php

All this function does is compare the current Action parameter with it's own ID. If they match it returns 'current' otherwise 'link' just like the $LinkingMode for pages!

The last thing left to do is include the sidebar on our template:

themes/blackcandy/templates/Layout/StaffPage.ss

So that's it! You now have a fully functioning DataObject display(er).

More about URLParameters

In this tutorial we only really scraped the surface of using URL parameters. There are 3 setup by default in SilverStripe, 'Action', 'ID and 'OtherID'. These can be accessed in the same way as this tutorial using Director:URLParam('ParamName'). They can also be accessed by assigning all of them to an array like so:

You can also define your own URL parameter structure using Director::addRules(), though documentation on the subject is sparse.

About the Author

Name: Aram Balakjian

Website: http://www.aabweb.co.uk

Aram is a web designer/developer running London based agency aab web. He has a strong passion for developing attractive, usable sites around the SilverStripe CMS.

Comments (10)

  • ini_set(\'display_errors\', 1);
    error_reporting(E_ALL);

    Posted by chanel bags, 29/09/2010 2:36am (4 years ago)

  • I wish you have a wonderful day!
    jiemo

    Posted by Chanel, 28/09/2010 3:23am (4 years ago)

  • I wish you have a wonderful day!
    jiemo

    Posted by Gucci, 28/09/2010 3:22am (4 years ago)

  • I wish you have a wonderful day!
    jiemo

    Posted by coach, 28/09/2010 3:21am (4 years ago)

  • I wish you have a wonderful day!
    jiemo

    Posted by Louis Vuitton, 28/09/2010 3:21am (4 years ago)

  • I wish you have a wonderful day!
    jiemo

    Posted by nike, 28/09/2010 3:02am (4 years ago)

  • http://http://www.findsoso.com/

    Our website wholesale for many kinds of fashion clothing, such
    as nike,jordan,prama,****, we also sell jeans,shirts,bags,hat and
    many kinds of tiffany. All the products are free shipping, and the
    the price is enticement , and also can accept the paypal
    payment.we can ship within 24 hours after your payment.

    accept the paypal

    free shipping

    competitive price

    any size available



    Everything is here http://www.findsoso.com friends, please
    enjoy viewing bar

    Thanks

    Posted by xihuan, 27/09/2010 10:26am (4 years ago)

  • I downloaded the files but when I click on the image the page cannot be found.

    Posted by Eric Downing, 21/09/2010 4:16pm (4 years ago)

  • mason0507

    Posted by Chanel, 16/09/2010 9:50pm (4 years ago)

  • mason0507

    Posted by supra, 16/09/2010 9:39pm (4 years ago)

1 2 3 4 next »

RSS feed for comments on this page RSS feed for all comments

Post your comment

Comments have been disabled. Please visit this post on SSBits.com to post your comment