Jump to:

23373 Posts in 18175 Topics by 2865 members

General Questions

SilverStripe Forums » General Questions » How does Silverstripe work?

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 725 Views
  • simples
    Avatar
    Community Member
    76 Posts

    How does Silverstripe work? Link to this post

    What happens when a browser sends a request to retrieve content from a Silverstripe web site?

    Specifically I want to know what controls what files are accessed under the mysite, themes and modules folders where module folders contain modules created by a user.

    I am asking this question because I would like to write my own modules and I do not know how to in a way which totally segragates the code into the module.

    I have read http://doc.silverstripe.org/framework/en/2.4/topics/module-development which I assume is a very brief summary which is supposed to direct readers to more detailed relevant information. However, unless I am missing something big time, it fails miserably.

    I have painstakingly gone through the tutorials and spent much time going through the books based on Versions 2.3 and 2.4. I am not looking for indepth details. I just want to have a basic idea of what controls which files are accessed to help me write well segregated, modular code.

    If no-one can help me tomorrow I plan to see if I can take the code apart starting with sapphire/main.php to see if I can suss out what happens when a request is received.

    PS. Just before I submitted this post I sent the following documents to my printer and read them. Unfortunately I still do not have a clear understanding of how things work.

    http://doc.silverstripe.org/framework/en/2.4/reference/execution-pipeline
    http://doc.silverstripe.org/framework/en/2.4/reference/advanced-templates

    Have I now exhausted where to look or have I overlooked a source?

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: How does Silverstripe work? Link to this post

    On a really low level there are two main objects I keep in mind DataObject & ViewableData.
    Examine those classes in detail. The Controller class extends RequestHandler which extends ViewableData. The Page class extends SiteTree -> DataObject -> ViewableData.

    In SS3+ everything is a module outside of the framework folder (cms, mysite). SS2.4 was basically the same but sapphire & cms folders where tightly coupled.
    To make a module make a new folder whack an empty _config.php file in the root of the new folder. The following conventions are then used for various directories, code, tests, templates.

    Other than that it's hard to go into more detail on module creation without knowing what you're trying to achieve. If you're trying to make a custom Controller to intercept requests or just make a new object for storing data. Or creating a decorator to apply to an existing object.

  • simples
    Avatar
    Community Member
    76 Posts

    Re: How does Silverstripe work? Link to this post

    Thank you for helpfully outlining to me what you keep in mind.

    My query comes from issues which I have faced when creating the following modules

    cssmenu
    slideshow
    localimages (to prevent hotlinking of images)
    homepreviews (to automatically place News article previews on the homepage)
    calendar

    One issue regarding segregation of code when adding a module which I have not been able to resolve is how to avoid the need to place code in my mysite Page class to retrieve data. For example I have just created a calendar module. I use this module to populate a div placed on my site's home page with calendar events. This is how I currently do it. Please don't laugh!

    In themes\mysite\templates\Layout\HomePage.ss I place the following code,

    <% include Calendar %>

    In module_calendar\templates\Calendar.ss, I place the markup for the Calendar including the div and the following control structure to retrieve the events.

    <% control returnCalendarEvents %>

    In module_calendar\code\Calendar.php, I place the returnCalendarEvents function in the Page class.

    Annoyingly the div does not get populated with the events unless I place the following code

    function returnCalendarEvents(){
    $myCalendar=new Calendar();
    return $myCalendar->returnCalendarEvents();
    }

    in the Page class at mysite\code\HomePage.php.

    Why do I need to do this? This means that my code is not modular. Do you know what I should be doing to avoid having to do this?

    I have posted all the code for the calendar module in case I have given an incomplete picture in the extracts shown above.

    Thanks.

    Attached Files
  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: How does Silverstripe work? Link to this post

    I have not been able to resolve is how to avoid the need to place code in my mysite Page class to retrieve data

    Checkout DataExtensions. These can be applied to core objects to add functionality (such as features onto Page) - http://doc.silverstripe.org/framework/en/reference/dataextension

  • simples
    Avatar
    Community Member
    76 Posts

    Re: How does Silverstripe work? Link to this post

    Hi Willr,

    Thank you for your reply. Can I just confirm that I understand the implications of this? My understanding of classes is week so I may have got things wrong.

    Does this mean that if a template in a published module contains a control structure, which for example retrieves data, the person installing that module will need to add code outside the module, such as in a mysite code class, before those control structures will work?

    If this is the case, I think that this is a shame because then the module is not a simple bolt-on since in order to get it to work, code has to be added outside the module. As such the added code is not segregated.

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: How does Silverstripe work? Link to this post

    Does this mean that if a template in a published module contains a control structure, which for example retrieves data, the person installing that module will need to add code outside the module, such as in a mysite code class, before those control structures will work?

    Absolutely not, as you point out that would be shame (and useless). Modules either provide custom Page types for the user (see Userforms) or bolt onto core class through DataExtension. A simple example would be

    <?php

    class MyPageExtension extends DataExtension {

    public getSomeNewData() {
    return Page::get();
    }

    Once adding that extension to your Page (through the Config system) your Pages could now do <% loop SomeNewData %>

    The best way to learn is to browse through some of the existing modules and take a look at the example code from the books.

  • simples
    Avatar
    Community Member
    76 Posts

    Re: How does Silverstripe work? Link to this post

    Hi Willr,

    That is great.

    I am developing a site which uses 2.4 and I plan to examine sample code in the 2.4 book. I see that searching this code using the phrase "extends DataExtension" returned no results so I think I need to use a different phrase.

    Thanks for your help.

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: How does Silverstripe work? Link to this post

    Note that if you're using 2.4 it's a DataObjectDecorator (not extension). For 3.0 documentation at http://doc.silverstripe.org/framework/en/reference/dataextension

    725 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.