Jump to content

Is it possible to access the ProcessWire API out of scope in another part of the site?


hipperman
 Share

Recommended Posts

Hey everyone. I've built an app on a website that has ProcessWire in only a narrow scope of the website. They're using ProcessWire's login for certain users in this particular section of the website. My app is in a completely different directory, outside the scope of ProcessWire. I'm hoping it's possible for me to import ProcessWire into my app's backend and find if a user is current logged in. Is there some way I can do that? Thank you!

Link to comment
Share on other sites

Hi,

you can bootstrap processwire in own PHP scripts following this howto https://processwire.com/docs/front-end/include/.

For sure you could include the PW index.php script in your PHP app to check if a user has logged into the PW part of your site. But it wouldn‘t provide lot of features to access non PW user credential infos like login data of your App outside PW etc. Unfortunately the infos are a bit weak so I can‘t become more precise as I haven‘t fully understood what you want to achieve or what data you want to access from the non PW part. 

  • Like 3
Link to comment
Share on other sites

Hello,

What @cwsoft said. 

Adding, for example, if you need using it in your own namespace, you can connect ProcessWire like this:

<?php namespace Foo\App;

use Foo\BarClass;
use ProcessWire\Page;

class PwConnector {

  public function init() {
    $this->bootstrapProcessWire();
  }
  
  protected function bootstrapProcessWire() {
    $pw_dir = '/var/lib/www/pw'; // example, processwire is installed here
    if (!function_exists('\ProcessWire\wire')) include($pw_dir . '/index.php');
    echo \ProcessWire\wire('pages')->get('/')->title; // echo home title
  }
  
  // ...
}

 

  

33 minutes ago, cwsoft said:

But it wouldn‘t provide lot of features to access non PW user credential infos like login data of your App outside PW etc.

He will still benefit an easy API to use to access them by using the $database object, if stored externally.

  • Like 2
Link to comment
Share on other sites

@hipperman

When you include() ProcessWire's /index.php file, that'll leave you with a $wire variable, which is the ProcessWire instance of the site you included the index.php file from. From there, you can access any ProcessWire API variables from that $wire variable, i.e. 

include('/path/to/your/pw/index.php');
$page = $wire->pages->get('/about/company-history/'); 
echo $page->body; 

If need be, you can even boot multiple different instances of ProcessWire, but just make sure they are all running the same version. 

  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...

@ryan 

Thank you for your response. I've successfully included ProcessWire's index.php file and have access to $wire. I'm trying to use $user->email to get the email of the currently logged in user. The issue I'm running into is it's just showing a guest user after I've logged in. It seems as if I'm just using a new instance of PW. 

Here is my websites directory structure:

root/pw/index.php

root/my-project/index.php

require_once $_SERVER["DOCUMENT_ROOT"] . "/pw/index.php";

$account = $users->find( "email=" . "hipperman@somecompany.com" );

print_r($account); // This is finding the user account
print_r($user); // This shows a guest account even though I have logged in

Any ideas? Thank you so much for your help. 

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