Jump to content

PW3 Problem calling Core API functions


FrancisChung
 Share

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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 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

×
×
  • Create New...