Jump to content

PW3 Problem calling Core API functions


Recommended Posts

I've updated my site's PW version from PW 2.6.x to the latest version.

I've updated manually by copying the wire directory and overwriting index.php and .htaccess.

When I try and load up the page I'm getting the following error.

Error: Call to undefined function Site\wire() (line 5 of /Users/FrancisChung/Sites/Develop/site/templates/php/const/Const.php) 

My Const.php looks like :

namespace Site;
require_once(__DIR__."/../../../../index.php");

Define(SITE_DIR, wire(config)->paths->templates);

Define(IMG_DIR , wire(config)->urls->templates."img/");
Define(IMG_SQDIR, wire(config)->urls->templates."img/square/");
Define(IMG_SDIR, wire(config)->urls->templates."img/small/");
Define(IMG_MDIR, wire(config)->urls->templates."img/medium/");
Define(IMG_LDIR, wire(config)->urls->templates."img/large/");
Define(IMG_PATH, SITE_DIR."img/");
Define(IMG_SQPATH, SITE_DIR."img/square/");

I'm guessing the wire(config) calls are failing because of the new Namespace implementation?
Is there a way of fixing this without having to change every wire(config) or other wire(fnXXX) calls?

Link to post
Share on other sites

For php 5.6+ by using this:

use function ProcessWire\wire;

Or maybe a own function wire() for your namespace, which then delegates to processwire's function:

function wire($key, …) {
	return \ProcessWire\wire($key, …);
}

And just to make that clear. Bootstrapping processwire will make api variables available as local variables, no need for wire() func calls here. Just use $config.

  • Like 1
Link to post
Share on other sites

I'm guessing there's not a PHP 5.5 solution for this problem?

Reason why I'm still using PHP 5.5 is because it's the latest version I could get XDebug + PHPStorm 2016.2 + MAMP Pro 3.4 to work nicely.
I've just tried again to see if I could get PHP 5.6 working but it didn't work.

I could upgrade to MAMP Pro 4 to see if it's a difference maker, but I have doubts it will actually fix my issue.

Link to post
Share on other sites
  • 3 weeks later...

Hi @LostKobrakai,

I'm having another crack at upgrading our website stack to PHP 7 and PW3.

I tried your suggestion of using 

Quote

use function ProcessWire\wire;

also tried adding a \ at the front.

In both instances, I get

Notice: Use of undefined constant SITE_DIR - assumed 'SITE_DIR' in /Users/FrancisChung/Sites/PHP7PW3/site/templates/php/const/Const.php on line 29
Call Stack

I've tried using $config and wire(config) and it's throwing up the same error. Any ideas?

I'm trying to access 

wire(config)->paths->templates
Link to post
Share on other sites

SITE_DIR is not defined because it is failing on the wire(config) or $config line of code.

I've tried the following debug code 

echo $config->paths->templates;
echo wire(config)->paths->templates;

and it's returning the following for both

Use of undefined constant config - assumed 'config' in /Users/FrancisChung/Sites/PHP7PW3/site/templates/php/const/Const.php on line 29

 

Apologies for any confusion in the previous post.

Link to post
Share on other sites
8 minutes ago, FrancisChung said:

Use of undefined constant config - assumed 'config'

That makes sense for sure with

echo wire(config)->paths->templates;

it should be:

echo wire('config')->paths->templates;

Not sure why you would get the undefined constant with the $config version though.

  • Like 1
Link to post
Share on other sites
<?php
namespace Site;
require_once(__DIR__."/../../../../index.php");



use function \Processwire\wire;


Define(SITE_DIR, $config->paths->templates);

Define(IMG_DIR , $config->urls->templates."img/");
Define(IMG_SQDIR, $config->urls->templates."img/square/");
Define(IMG_SDIR, $config->urls->templates."img/small/");
Define(IMG_MDIR, $config->urls->templates."img/medium/");
Define(IMG_LDIR, $config->urls->templates."img/large/");
Define(IMG_PATH, SITE_DIR."img/");
Define(IMG_SQPATH, SITE_DIR."img/square/");

Define(LOGO_PATH, IMG_DIR."linguino-wortbild_weiss_fb_twothirds.png");

Define(DEV_SITE, "kinder-reime.com");
Define(UAT_SITE, "finger-spiele.com");
Define(LIVE_SITE, "sprachspielspass.de");

It's failing on the first DEFINE statement where it doesn't know what $config is.

Link to post
Share on other sites
1 minute ago, adrian said:

That makes sense for sure with


echo wire(config)->paths->templates;

it should be:


echo wire('config')->paths->templates;

Not sure why you would get the undefined constant with the $config version though.

Once again, bitten by PHP's idiosyncrasies.

Not sure why wire(config) worked with older version of PHP (5.5) but not newer. Thanks Adrian for the quick response

Link to post
Share on other sites
5 minutes ago, LostKobrakai said:

Are you sure the require is correct? Bootstrapping pw does make all api variables available as local variables and that behavior hasn't changed in pw3.

Yes, that is 1 of the first things I cross checked.

I assume you mean the Index.php file in the root directory.

Also double checked to make sure it's V3.

Link to post
Share on other sites

Maybe I am missing something here, but aren't you causing yourself a world of extra pain by declaring a custom namespace (Site). Any reason not to use ProcessWire ?

Link to post
Share on other sites

And the file is called via the cli or other external way? I mean $config should get defined in the index.php, as long as it's not run as part of a direct webrequest.

I've just tried this on a local installation and there's no problem:

<?php namespace Site;

require_once "index.php";

echo $config->urls->templates;

Also as long as we're talking about variables and not class/function calls the namespace can't be the issue, as it's not affecting any variables.

Link to post
Share on other sites
10 minutes ago, adrian said:

Maybe I am missing something here, but aren't you causing yourself a world of extra pain by declaring a custom namespace (Site). Any reason not to use ProcessWire ?

Coming from the .NET world, I'm used to using Namespaces and it's pretty much defacto but was surprised it wasn't as widely adopted by PHP Developers.

So I thought I was doing the right thing by using a custom namespace in case there's any name clashes with other libraries I use.

How would I resolve name clashes if I used \Processwire ? 

Edited by FrancisChung
Incorrect syntax for NS
Link to post
Share on other sites
3 minutes ago, LostKobrakai said:

And the file is called via the cli or other external way? I mean $config should get defined in the index.php, as long as it's not run as part of a direct webrequest.

I've just tried this on a local installation and there's no problem:


<?php namespace Site;

require_once "index.php";

echo $config->urls->templates;

Also as long as we're talking about variables and not class/function calls the namespace can't be the issue, as it's not affecting any variables.

There are some modules that have dependencies on Const.php.

It's loaded via require_once in all of them

require_once(__DIR__."/../const/Const.php");

 

Link to post
Share on other sites

That's the issue. You cannot bootstrap pw, when it's already running. Also starting the pw instance again would be a waist of computation anyways.

<?php namespace Site;

$config = \ProcessWire\wire('config');

[your defines]

 

  • Like 1
Link to post
Share on other sites

I think I know what I did here. 

I think 1 of the first modules I wrote was a standalone module that needed access to the API.
Also happened to be the first module I wrote that accessed the API.

So because it worked, I just assumed it was the correct way of calling the API without actually realising what I was doing.

Should pay attention more to the documentation, but I guess it's sometimes hard when you're on super tight deadlines, reading/skimming so much material and working with new technologies you haven't got a clue about or lean on mentors for advice and validation.

  • Like 1
Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By prestoav
      Hi all,
      I could have sworn I used to be able to use the site generic 'title' field as a sub field of a repeater field. However I've tried to do this on two 3.0.165 sites recently and, while it will add the title field in the repeater field setup, it wont save the repeater title sub field's content when the repeater is used in a page template and edited.
      It;'s not a big issue but I wondered if this was a known restriction?
    • By opalepatrick
      I am working on my first Process Module. I am creating forms. Fairly straightforward. However, I really can't work out how to create multiple fieldsets?
      $fieldset = $this->modules->get('InputfieldFieldset'); $fieldset->label = 'Customer Source'; $field = $this->modules->get('InputfieldPage'); $field->inputfield = 'InputfieldSelect'; $field->findPagesSelector = 'parent_id=1449, include=hidden'; $field->labelFieldName = 'yff-lead'; $field->name = 'yfflead'; $field->columnWidth = 16; $fieldset->add($field); $field = $this->modules->get('InputfieldPage'); $field->inputfield = 'InputfieldSelect'; $field->findPagesSelector = 'parent_id=1452, include=hidden'; $field->labelFieldName = 'customer-type'; $field->name = 'customertype'; $field->columnWidth = 16; $fieldset->add($field); //Rinse and Repeat $fieldset->label = 'Contacts'; $field = $this->modules->get('InputfieldPage'); $field->inputfield = 'InputfieldSelect'; $field->findPagesSelector = 'parent_id=1538, include=hidden'; $field->labelFieldName = 'salutation'; $field->name = 'salutation'; $field->columnWidth = 16; $fieldset->add($field); I can create the first fieldset (Customer Source) but then get into trouble as the second fieldset overwrites the first. I understand why, but trying to use the open and close fieldset routine has flummoxed me. Any help appreciated.
    • By VeiJari
      Hello forum, we're trying to use Processwire as our REST-API. We are having problems with our API login to Processwire from frontend. It gives us 403 error.
      We have installed ProcessWire to subdirectory (/api/*) and our frontend is static JS files at root ( / ). Apache access logs gives 404 to our POST-request, but browser devtools shows 403 for our POST /api/login request. 
      Processwire backend panel works. We also have a GET endpoint for the API that returns 200 with correct payload.  So we're wondering why does our GET works but POST doesn't?
      Does this have something to do with Processwire .htaccess, or is this because of our webhost? What should we check first? Any help would be appreciated.
    • By rjgamer
      Hi,
      is there a hook after the current (active) page got created? Or which method got called in the Page class after the Constructor of the current page got initialized?
      Thanks.
       
    • By killedfriendz
      I am very sorry for asking this but i totally do not understand how to set values of checbox using API. 
      I have checbox field on my page with name "order_status". 
      So i've tried few ways to make it checked but it still doesn't work:
       
      $userPage->order_status->value = 1; $userPage->order_status->add(1); $userPage->order_status->add(true); Could you please tell me how to do it?
×
×
  • Create New...