Jump to content
modifiedcontent

Post to database of another Processwire site

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?

Share this post


Link to post
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

Share this post


Link to post
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?

 

Share this post


Link to post
Share on other sites
4 hours ago, modifiedcontent said:

$skyscrapers = $forum->pages->find('parent=skyscrapers, limit=3');

Forgot to show where $forum is defined in your example code? Would be good to show the complete, correct code to help others, thanks.

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites

Oops, kongondo. $forum should have been $mainsite. Now corrected in the example.

@ Sergio, I know that, but is there a way to make that work from a "sister site" with this method?

$mainsite->user->isLoggedin(); doesn't work.

  • Like 1

Share this post


Link to post
Share on other sites
2 hours ago, modifiedcontent said:

$mainsite->user->isLoggedin(); doesn't work.

Yeah, I can confirm this. From my testing ,$mainsite does not seem to have access to $session. Security? I don't know, just guessing.

  • Like 1

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
1 hour ago, modifiedcontent said:

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:

Similar(-ish) issue?

 

Share this post


Link to post
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?

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 jploch
      Hey folks,
      Iam working on a module that extends InputfieldPageTable. I use PageTableExtended as a starting point and all went well so far, except I can't access my config values outside of the module file. The config values allways return the defaults, and not the values in the pw field settings. 

      Here is the code that sets the defaults inside my inputfield module file:
      public function init() { parent::init(); // defaults $this->set('gridMaxWidth', '1600'); $this->set('gridColumns', '12'); } Here is how I add the config field:
      public function ___getConfigInputfields() { $inputfields = parent::___getConfigInputfields(); $fieldsetGrid = $this->modules->get('InputfieldFieldset'); $fieldsetGrid->label = $this->_('Default Grid Settings'); $f = $this->wire('modules')->get('InputfieldText'); $f->attr('name', 'gridMaxWidth'); $f->attr('value', $this->gridMaxWidth); $f->label = $this->_('Container max width'); $f->description = $this->_('Grid container max width in px'); // page name format description $f->notes = $this->_('default is 1600'); // page name format notes $fieldsetGrid->append($f); $inputfields->append($fieldsetGrid); return $inputfields; } Accessing the values outside my module files like this (this gives me the default: 1600, even if I insert another value in the backend field settings):
      $default = $this->wire('modules')->get('InputfieldPageGrid'); $default->gridMaxWidth; Any Ideas?
    • By ngrmm
      My clients wants a modal to show up on every page. But when a user clicks inside the modal -> a session-cookie is set and the modal gets a class.
      // user clicks on modal button $('.modal_button').click(function(){ // 1. set PW session cookie // 2. toggle class $('.modal').toggleClass('off'); }); I know how to set a cookie on page-load via PW-API. But the click on the modal button does not force a page-load. So i have to set the cookie through javascript. Is there a way to do that?
    • By VeiJari
      Hello,
      I'm trying to create a page via api and populate values to it. I can populate everything except user pages to a page reference array.
      Code: 
      $dataUsers = $data->project->users; foreach($dataUsers as $dataUser) { $newProject->projectUsers->add(wire()->pages->find('template=user, id=' . $dataUser->id)); } I'm receiving my data via JSON.
      Is there something I'm missing?
      Thanks for help
    • By VeiJari
      Hi, this is the first we are trying to make a page that has only one type of user that has access to every page. 
      The other users should only have a given access to specific pages, not to the whole template.
      My structure
      -Field -Organisation -Project -Report I want that the "measurer" role only has access to "project x" and it's children, but no view access to every project, organisation or field. I've tried to do this with https://modules.processwire.com/modules/page-edit-per-user/ but it still needs a view access to the whole tree to see the "project x" page. Or is there something I haven't figured out?
      Maybe I have to make it via the API: a select field in the "organisation" template where the admins could add the users and then I use hook to update the privileges?
      Have you done something like this and how did you accomplish it?
      Any help would be appreciated.
       
    • By cosmicsafari
      Hi all,
      Before I go potentially wasting time trying to achieve the impossible.
      Can anyone confirm if its possible to have a Page Reference field on a modules config page?
      I'm wanting to essentially just output a list of select able pages based on the a given selector (likely by template at this stage), wherein the select is the pages that the module should apply to etc. I was thinking a simple checkbox list would suffice is asmSelect isn't available.
      Essentially have it display the same way a Page Reference field would display on a template, where you can easily select a bunch of them.
      public function getInputfields() { $inputfields = parent::getInputfields(); $f = $this->modules->get('InputfieldPage'); $f->attr('name', 'testSelect'); $f->setAttribute('multiple', 'checkboxes'); $f->setAttribute('findPagesSelector', 'template=development'); $f->label = 'Test'; $inputfields->add($f); return $inputfields; } Figured something akin to the above would work but can't seem to get rid of this warning on the modules config screen though.

×
×
  • Create New...