Jump to content

Image paths when bootstrapping PW?


alkahest
 Share

Recommended Posts

I'm having a weird situation where I'm trying to get the URL for an image attached to a page, but I'm getting back the wrong paths.

The URL is mysite.com/5gum/. My file structure is like this:

/admin/ <-- ProcessWire is in here
-- /site/
-- /wire/

/5gum/ <-- my site templates are in here 
-- index.php (calls header.php below)

/assets/ 
-- /includes/ 
---- header.php

I'm bootstrapping PW into header.php of a file under /5gum/.

I'm trying to do something like this:

$brand_slug = basename ($_SERVER['REQUEST_URI']);  // this would get me "5gum"
$brand = wire('pages')->get("name=".$brand_slug); // this gives me a specific page
echo $brand->meta_image->url; // this should output the path to an image attached to the page

If meta_image is a field that accepts a maximum of 1 image, shouldn't this return the full URL to the image? Instead, it returns:

/5gum/site/assets/files/1027/

What doesn't make sense to me is that the root of the site isn't /5gum/, it should be /admin/ right, according to PW? Do I need to set the root somehow?

Link to comment
Share on other sites

I don't really get your setup. Pretty exotic.

What about /site/templates/ ?

I'm not sure, but if you don't have your PW installed in a subfolder /5gum/ then it shouldn't include in it urls. But as I said I don't really get your setup here.

Could be that you have the /5gum/index.php rendering something? Why do you have it this way?

To your problem of the image url. It's simply because you do this in a bootstrap and there's no output formatting happening there. So if there's no output formatting any image field will be an array, regardless of the image field settings (set to max 1).

So this will get your image

echo $brand->meta_image->first->url;
  • Like 1
Link to comment
Share on other sites

Ahhh, that does the trick! When I do $brand->meta_image->first->url, I get the filename as well.

However it still outputs /5gum/ as the root:

/5gum/site/assets/files/1033/slim_cobalt.png

I know my setup is strange, basically there is a brand under each directory in the top-level domain:

/5gum/

/some-other-brand/

/another-brand/

And they all share the same assets (images, markup, etc) from the /assets/ folder. By visiting /5gum/, the system pulls in all the includes it needs from /assets/includes/ like the footer.php and header.php and other files. However all PW is managing in this case is order information, so that's why it's bootstrapped in and cordoned off in its own folder under /admin/. I'm not using any template file output from PW, I just talk to PW via Ajax to send/retrieve order information (and some basic information about the products).

What I suspect is that somehow PW thinks /5gum/ is the root of the site. In fact, when I ask for:

$config->paths->root

PW says the root is /5gum/ as opposed to /admin/. Is there a way to tell PW what the root of the site is?



(And yes, if you go to mysite.com/5gum/ you hit index.php, which calls in header.php, which is where PW is bootstrapped.)

Link to comment
Share on other sites

PW is looking at the request URL. Since your request URL is /5gum/, PW thinks that's the starting point. You may need to set $config->urls->root manually to /admin/.

This bit of code is probably not so safe: 

$brand_slug = basename ($_SERVER['REQUEST_URI']);  // this would get me "5gum"
$brand = wire('pages')->get("name=".$brand_slug); // this gives me a specific page
I'd suggest changing it to:
// sanitize brand slug with the pageName function
$brand_slug = wire('sanitizer')->pageName(basename($_SERVER['REQUEST_URI']));  // this would get me "5gum"
$brand = wire('pages')->get("name=$brand_slug"); // this gives me a specific page

// double check that brand is what you expect and that it's viewable
if(!$brand->id || $brand->template != 'brand' || !$brand->viewable()) 
  throw new Wire404Exception(); 
  • Like 1
Link to comment
Share on other sites

  • 5 years later...

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