Jump to content

Server error when retrieving data from a class


MichielW
 Share

Recommended Posts

Hello! I'm pretty new to Processwire and I'm trying to retrieve data from inside a class, but nothing seems to work ? With the posted approach (including the namespace) the error message says it can't find the class GetContent. Wihtout the namespace it gaves a 'call to a member function get() on null'. Also using $this-> or not doesn't seem to make a difference.

When putting $pages->get("/info)->title; outside the class, it does work (but that's obviously not what I want). The "/info" is hardcoded, just to isolate the problem.

I pasted the error messages at the bottom.

=== CODE ===

<?php namespace Processwire;

require_once("../defaultpwsite/index.php");

class GetContent
{

    
    public function getTitle($req_page)
    {

      $page_title = $this->pages->get("/info")->title;
      return $page_title;
    }
    
    public function getContent($req_page)
    {
        $page_content = $this->pages->get("/info")->textblock;
        return $page_content;
    }
}

=== ERROR 1 (NOT USING NAMESPACE)

Fatal Error: Uncaught Error: Call to a member function get() on null in /Applications/mampstack-7.0.31-0/apache2/htdocs/simplesite/php/model/getcontent.model.php:26 Stack trace: #0 /Applications/mampstack-7.0.31-0/apache2/htdocs/simplesite/php/view/page.basesite.htmldoc.view.php(21): GetContent->getContent('home') #1 /Applications/mampstack-7.0.31-0/apache2/htdocs/simplesite/php/controller/page.controller.php(37): PageContent->__construct('home') #2 /Applications/mampstack-7.0.31-0/apache2/htdocs/simplesite/php/controller/main.controller.php(42): PageController->showResponse() #3 /Applications/mampstack-7.0.31-0/apache2/htdocs/simplesite/index.php(25): MainController->handleRequest() #4 {main} thrown (line 26 of /Applications/mampstack-7.0.31-0/apache2/htdocs/simplesite/php/model/getcontent.model.php)

=== ERROR 2 (USING NAMESPACE)

Fatal Error: Uncaught Error: Class 'GetContent' not found in /Applications/mampstack-7.0.31-0/apache2/htdocs/simplesite/php/view/page.basesite.htmldoc.view.php:21 Stack trace: #0 /Applications/mampstack-7.0.31-0/apache2/htdocs/simplesite/php/controller/page.controller.php(37): PageContent->__construct('home') #1 /Applications/mampstack-7.0.31-0/apache2/htdocs/simplesite/php/controller/main.controller.php(42): PageController->showResponse() #2 /Applications/mampstack-7.0.31-0/apache2/htdocs/simplesite/index.php(25): MainController->handleRequest() #3 {main} thrown (line 21 of /Applications/mampstack-7.0.31-0/apache2/htdocs/simplesite/php/view/page.basesite.htmldoc.view.php)

 

 


       

Link to comment
Share on other sites

Hello @MichielW and welcome to the forum and PW.

The global PW variables (like $pages) are not directly accessible out of a class, you'll have to go through the wire() method or wire() function.

$page_content = $this->wire('pages')->get("/info")->textblock;

or

$page_content = wire('pages')->get("/info")->textblock;

Depending on your use context it probably would be better to hand over the $page via parameter (or cache its value in your class instance), to reduce re-fetching the same content from database with each call.

Link to comment
Share on other sites

Thanks a lot! I finally got it running. It also seemed the namespaceing on the top didn't work out for me, placing in front of 'wire()' did the job: 

     $page_title = \ProcessWire\wire('pages')->get($this->req_page)->title;

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...