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)