0

Silverstripe: I have a blog on my page with muliple pages, and 10 entries per page. I want my controller to know which page of the blog is currently being viewed.

I have the following code:

public function dependsOnPage() {

    $page = getPageHere; /*How can I get which page of the blog I'm currently on?*/

    $i = ($page*10)-10;

    echo $i;    
}

The URL on the second page would look like this for example: ?start=10 On the third like this ?start=20

Does anyone know how I can do this in Silverstripe?

I tried:

    //echo SiteTree::get_by_link('news')->Link();

    //echo (Director::urlParam());

    //echo Director::getCurentPage();

    echo Director::get_current_page();

    echo Director::baseURL();

    $test = $_GET['url'];
    echo $test;

    echo $this->request->param('ID');

    // echo Director::absoluteURL('', false);

    //echo $this->getCurentPage();

    //echo $this->request->param() ;

    //echo $this->URLSegment;

    //echo Director::absoluteBaseURL();

    //echo $this->param();

    //echo $this->getURLParams();

    //echo SS_HTTPRequest->param();

Solution:

$urlParam = Controller::curr()->getRequest()->getVar('start');

Rence
  • 2,900
  • 2
  • 23
  • 40
  • See http://stackoverflow.com/questions/1586330/access-get-directly-from-javascript nothing was marked as an answer but perhaps you can try something of it. You can also check here: http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript – Niddro Feb 12 '15 at 12:25
  • Thank you but sadly my problem has nothing to do with javascript. I'm trying to get the page on which my php function is executed directly from the controller. Most sites suggested (Director::urlParam(); but Silverstripe says the urlParam function is undefined. – Rence Feb 12 '15 at 12:30
  • Have you tried `$id = $this->request->param('ID');` instead of `$id = Director::URLParam('ID');` inside your controller? [Source](http://www.silverstripe.org/community/forums/general-questions/show/18118) – Niddro Feb 12 '15 at 12:38
  • Yes, that gives Undefined property: $request. I'll update my question with everything I tired. – Rence Feb 12 '15 at 12:48

1 Answers1

3

We can retrieve Post and Get variables with some functions on the SS_HTTPRequest object. We can get the SS_HTTPRequest object from our Controller.

SS_HTTPRequest has a function getVar($variableName) to retrieve Get variables.

If your function is in your controller you can call the following to get your $_GET variables:

$startValue = $this->getRequest()->getVar('start');

If your function is in your object class you first need to get the page controller before you get your $_GET variables:

$startValue = Controller::curr()->getRequest()->getVar('start');
3dgoo
  • 15,716
  • 6
  • 46
  • 58
  • I already solved it and posted the solution in my question, it's exactly the same as yours ($urlParam = Controller::curr()->getRequest()->getVar('start');) I will still mark this as the answer though so it's closed. – Rence Feb 13 '15 at 08:06