noelboss

[SOLVED] Extend API with httpUrl()

Recommended Posts

Maybe I'm missing something here, but I'm haveing a hard time to get absolute urls if PW is installed in a subdirectory thought the API...

I need an absolute URL for the backend (as a redirect url of Oauth provider). To build the url i would normaly use

url()->root.url()->admin – but this results in the install-path beeing present twice;

domain.com/path/ + path/admin/

In order to not use $_SERVER['HTTP_HOST'] I fetch the admin page from pages – but this is also not to easy since I need to remove the root folder from path as well. My current solution is somewhat complicated:

$backendUrl = pages()->get('path="'.str_replace(urls()->root, '', urls()->admin).'", include=all')->httpUrl;

The above code seems to be the savest way to get the absolute url to the backend using pure PW API, am I right?

It would be great if one could fetch absolute urls with something like this

$backendUrl = urls()->admin->httpUrl
// or
$backendUrl = httpUrls()->admin

 

SOLVED:

Thank you @adrian – of course this HAD to be simpler:

urls()->httpAdmin;

 

Share this post


Link to post
Share on other sites

The admin page always has the ID of 2 (as far as I know). So you can get the httpUrl with:

$pages(2)->httpUrl()

 

Share this post


Link to post
Share on other sites
20 minutes ago, Robin S said:

The admin page always has the ID of 2 

True..

Maybe better to get it like this...(in case down the road someone wonders what the 2 is all about, etc)..:)

$pages($config->adminRootPageID)->httpUrl();

 

  • Like 2

Share this post


Link to post
Share on other sites

Hahaha, fool I was, thinking PW not already soled it has! ;)

Once more, what a great community, THANK YOU!

API Cheatsheet would trueöy be great with these tibits...

So the solution is this:

urls()->httpAdmin;

 

Share this post


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 modifiedcontent
      I have one central website, with membership registration and content etc., and then several related websites with their own URL/domains, each on Processwire, all on the same server.
      I would like to access the database of the central website from the sister websites. How would I do that?
      You can't bootstrap one PW installation into another. You can include template parts from one in the other by just using the server path, but whatever you try to get/post just comes/goes to the database of the site you are on.
      Could you switch databases by including the config.php from another PW installation somewhere?
      What is the correct, secure way to do this?
    • By celfred
      Hello,
      I have a feeling I'm missing an easy thing here (again...). If I trash a page from API with :
      mypage->trash()
      Is there a simple way to restore it if needed ? I can do it in back-end, but I'd like to do it through API with something like
      mypage->restore()
       but this doesn't exit  
      So if you can give me a little help on that, I'd appreciate. Thanks in advance !
    • By Rudy
      Hi all,
      Is there a way to turn off fatal error notification without nullifying or commenting out: 
      $config->adminEmail Thx
      Rudy
    • By jrtderonde
      Hey,
      I'm working around a module that adds pages in my ProcessWire installation. The pages are added based on a JSON array that will be imported through a page save. Everything works fine except for when the pages are added. I get the following error
      Integrity constraint violation: 1062 Duplicate entry '3e215ecd6774fd99c2b0eb5cadf36a07-1269' for key 'name_parent_id' I'm using the following code/loop to generate the pages.
      // Loop through the files foreach ($p->importFile as $file) { // Set the file location $name = $file->data["basename"]; $path = $p->importFile->path; $location = $path . $name; // Get the file $json = file_get_contents($location); $json = json_decode($json); // Loop through the rows of the import foreach ($json->ttEntityDelAddrLink as $client) { // Create new page $new = new Page(); // Create unique hash $unique = md5(date("Y-m-d H:i:s") . "-" . $client->CustomerCode); // Set some variables for the new page $new->setOutputFormatting(false); $new->template = "_client"; $new->parent = $p; // Create hash $new->title = $client->DelAddressName; $new->name = $unique; // Page specific fields $new->company = $client->DelAddressName; $new->companyId = $client->CustomerCode; $new->city = $client->DelAddressCity; $new->address = $client->DelAddressStreet; $new->postcode = $client->DelAddressZipCode; $new->country = $countries[$client->DelAddressCountryCode]; // Save the page $new->save(); } // Exit for debugging exit; }
      Does anybody know what's wrong?