Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

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

How does Silverstripe work?


Go to End
Reply


10 Posts   868 Views

Avatar
simples

Community Member, 77 Posts

30 May 2013 at 1:31pm

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 [url]http://doc.silverstripe.org/framework/en/2.4/topics/module-development[/url] 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.

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

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

Avatar
Bambii7

Community Member, 254 Posts

31 May 2013 at 1:14pm

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.

Avatar
simples

Community Member, 77 Posts

1 June 2013 at 12:53am

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
Avatar
Willr

Forum Moderator, 5513 Posts

2 June 2013 at 2:55pm

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

Avatar
simples

Community Member, 77 Posts

2 June 2013 at 5:54pm

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.

Avatar
Willr

Forum Moderator, 5513 Posts

2 June 2013 at 6:12pm

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.

Avatar
simples

Community Member, 77 Posts

2 June 2013 at 7:17pm

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.

Avatar
Willr

Forum Moderator, 5513 Posts

4 June 2013 at 5:41pm

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

Go to Top