Jump to content

$session object unavailable in PHP function in template


ErikMH
 Share

Recommended Posts

So, if I do this right at the top of a template PHP file:

<?php
	namespace ProcessWire;
	function myFunction() {
		global $config;
		echo $config->dateFormat;
	};
?>

I, predictably, get this in my front end (once I figured out about the necessary global $config; line):

Y-m-d H:i:s

Happy camper. But if I try to access $session variables in the same way:

<?php
	namespace ProcessWire;
	function myFunction() {
		global $session;
		echo $session->future;
	};
?>

I’m told there is no $session object; it responds exactly as if I hadn’t included the global $session statement:

PHP Notice: Trying to get property 'future' of non-object in .../public/site/templates/home.php:5

I thought the problem might conceivably have to do with my private variables, so I tried the provided hasCookie() method:

<?php
	namespace ProcessWire;
	function myFunction() {
		global $session;
		print_r($session->hasCookie());
	};
?>

With essentially the same result:

Error

Call to a member function hasCookie() on null

$session variables and methods are fully accessible and work exactly as expected if placed immediately outside of myFunction().

I am rapidly running out of hair. Is this intentional? Or a bug? Can anyone confirm that they see this behavior? Can anyone think of a workaround? Thanks!

ProcessWire: 3.0.179
PHP: 7.4.19
 

Link to comment
Share on other sites

Just read this quickly...

  1. ProcessWire variables such as $config, $session, $page, etc need to be accessed differently inside functions (check out PHP function scope). One way to access them is via wire, e.g. wire('config'), wire('session'), wire('page'), etc.
  2. ProcessWire variables are already global. I don't understand why you are using the PHP global keyword.

Others will chime in, with better answers 😀.

  • Like 2
  • Thanks 1
Link to comment
Share on other sites

Thanks for your response, @kongondo! To answer your second point, I had indeed thought that ProcessWire variables were global — but since neither the $config nor the $session variable actually were accessible in my function (see the errors), I researched and found that:

Quote

A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function

— https://www.w3schools.com/PHP/php_variables_scope.asp

That source then went on to explain that that’s what the “global” statement is for. And in fact, adding “global $config” to the function solved the problem for $config. But when “global $session” did not solve the problem for $session, I posted here looking for guidance.

Thank you for the pointer to wire, which I’ve never heard of and which didn’t come up in the entire day that I spent searching the PW docs, blogs, tutorials, and forum. I’ll experiment with wire('session') and see whether that solves my problem.

I’m loving ProcessWire itself, but the layers upon layers of documentation and forum posts and blog articles that you have to wade through in hopes that someone has been kind enough to provide a link to the information that you really need is very frustrating.

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.

  • Similar Content

    • By RyanJ
      I have quite the pickle. 
      The admin no matter where I am at in it, times out constantly. I either get a 504 gateway or and execution timeout. I am banging my head why its across the board. The frontend of the site works great.
      The site does have a lot of pages, 164,889 to be exact.  Most of that is due to image pages created by media manager and having repeater matrix installed which is used on most of the pages. For some reason, if I can manage  click the "Clear file compiler" without it timing out, the site perks back up and I can access the admin again and it is quite speedy. However if I walk away leaving the browser sitting for about an hour and come back, I have to start the process all over again.
      The site has 52 various fields. Not every field is assigned to every template. a lot of the fields are used by the matrix to create a page builder.
      I can't accept that PW can't handle such. I have other sites that run triple the amount of pages, but don't rely on the matrix to build the layout.
      I have disabled any custom module that I thought could possibly cause an issue. I have overwrote every module and the wire core thinking something may have got corrupted. I guess my next step is starting with a fresh install, but I thought I would reach out to see if I am missing something. 
      Some background on pages:
      All the pages are imported using the command line which bootstraps PW and uses the api. This is a dev site, so I blow away the pages if I need to reimport them, until I get it correct.
      PW Version 3.0.172, PHP 7.3, Innodb 5.7.27-30.
      I appreciate any suggestions or thoughts. 

       

       

    • By dfile
      Hello,
      I am running ProcessWire 3.0.165 but $input->urlSegmentLast or $input->urlSegment(-1) does not work as expected.
      Or do I something wrong?
      Thank you.

    • By bramwolf
      Hi Guys,

      I found here: https://processwire.com/docs/selectors/#finding2 that I can exclude results from my $pages->find($selctor); query by using parent!=123 ( parent ID ) But I was wondering if I can
      also do so based on the parent template? I've tried doing it by itterating over the matches array and dropping the matches with the parent but since the result is limited for use with the paginator
      it only drops the items from the batch displayed on this page. Say from the results within matches 0 - 20 and not the entire 0 - 220.

      Anybody have a idea of how to achieve this? 🙂

      Thanks in advace!
      Bram 
    • By LAPS
      I've pages using the custom-page-template and I would like to display a simple message at the top of the page form when editing those pages in the Admin.
      I thought about hooking ProcessPageEdit::buildForm() but I do not well how to use that hook in order to display the message just for the custom-page-template pages.
      Any help?
       
       
      UPDATE: Solution found
      wire()->addHookAfter('ProcessPageEdit::buildForm', function(HookEvent $event) { $ProcessPageEdit = $event->object; $form = $event->arguments('form'); switch ($ProcessPageEdit->getPage()->template->name) { case 'custom-page-template': $form->prependMarkup("<div style='margin-bottom: 3.6em; margin-top: 1em; background: #ffd;'>MY MESSAGE</div>"); break; default: // do nothing } }); Any improvement is welcome!
    • By Sten
      Sorry I can't find any submit button to save my configuration on a template in the admin interface. The file is fine.
×
×
  • Create New...