Jump to content

Post to database of another Processwire site


modifiedcontent
 Share

Recommended Posts

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?

Link to comment
Share on other sites

1 minute ago, modifiedcontent said:

I vaguely remembered hearing something about this, but couldn't find it

Yep, that's how it is often. Also I tried to bookmark all interersting stuff (with several systems of subfolders), it ever ended up unusable, just because of the amount of  bookmarks collected by the time. :rolleyes:

  • Like 1
  • Haha 1
Link to comment
Share on other sites

I kinda use this forum to keep track of how-to's and things I need. Your response will probably be useful to others looking for the same kind of thing.

Tested it. Very cool. Dumping this into a template of another PW installation on the same server works:

<?php 
$mainsite = new ProcessWire('/home/serverpath/public_html/mainsite/site/', 'https://mainsite.com/');

if($input->post->createuser) {
$item = $mainsite->users->add('fred');
echo 'hello ' . $item->name;
}

$skyscrapers = $mainsite->pages->find('parent=skyscrapers, limit=3');
foreach($skyscrapers as $skyscraper) {
  echo '<h4>' . $skyscraper->title . '</h4>';
}
?>

<form method=post>
<input type=submit name=createuser value='add Fred to the main site'>
</form>

Fred gets added as a user to the main site. Pulling in content from the main site is straightforward.

Is there a way to check if a user is loggedin on the main site?

 

Link to comment
Share on other sites

Saving a complete user object also doesn't seem to work. When I try something like this, only the username gets saved, none of the other fields - except maybe addRole:

$u = new User();
$u->of(false);
$u->firstname = $sanitizer->text($input->post->firstname);
$u->lastname = $sanitizer->text($input->post->lastname);
$u->name = $sanitizer->text($input->post->name);
$u->email = $sanitizer->email($input->post->email);
$u->registrationDate = time();
$u->addRole('');
$mainsite->users->save($u);

Or am I doing something wrong?

This works:

$newuser = $mainsite->users->add();
	
$u = $mainsite->users->get( $newuser->id );
$u->of(false);
$u->firstname = $sanitizer->text($input->post->firstname);
$u->lastname = $sanitizer->text($input->post->lastname);
$u->fullname = $u->firstname.' '.$u->lastname;
$u->name =  $sanitizer->text($input->post->username);
$u->email = $sanitizer->email($input->post->email);
$u->registrationDate = time();
$u->addRole('');
$mainsite->users->save( $u );

 

  • Like 1
Link to comment
Share on other sites

10 hours ago, modifiedcontent said:

$newuser = $mainsite->users->add(); $u = $mainsite->users->get( $newuser->id ); $u->of(false); $u->firstname = $sanitizer->text($input->post->firstname); $u->lastname = $sanitizer->text($input->post->lastname); $u->fullname = $u->firstname.' '.$u->lastname; $u->name = $sanitizer->text($input->post->username); $u->email = $sanitizer->email($input->post->email); $u->registrationDate = time(); $u->addRole(''); $mainsite->users->save( $u );

Excellent!

I don't think you need $u->of(false) for a new record/page, btw.

Btw2, I testing saving a field in an existing $mainsite->somePage and it worked OK. However, I noticed two things:

  1. I expected PW to throw an error if I attempted to save the page without turning output formatting off, but it didn't
  2. Reloading the page where I was viewing content from $mainSite->somePages, the changes made to the above field were only visible after a 2nd reload

Did you experience this @modifiedcontent?

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