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 Guy Incognito
      This short script loops through some images from an XML feed and pushes new ones to an image field. It all works perfectly, except for some reason the last image (only) in the loop each time doesn't receive the image description... can everyone spot why? TIA! 🙂 
      foreach ($propertyImages as $img) { $fileName = trim($img[0]); if ( !empty($fileName) ) { $imgPath = '../property_data/'.$fileName; if(file_exists($imgPath) && !in_array(strtolower($fileName),$currentImages)) { $p->property_images->add($imgPath); $p->save(); $newImg = $p->property_images->last(); $newImg->description = $img[1]; $p->save(); } } }  
    • By MateThemes
      Hello everyone!
      I am working with Processwire since some time. But some topics are quite hard for me.
      I have a Portfolio (Gallery) Page.
      I am build a template with Portfolio Index and pages with portfolio entries.
      Structure:
      Portfolio Index
      -- Portfolio Entry
      -- Portfolio Entry
      and so on.
      Portfolio Entry has an Image field with max 12 images and are accessible Templates. 
      Now I want to display the single Portfolio Entry on the Portfolio Index and Paginate them. In the index page all images of a single Entry page should be displayed (I should not be organized as albums, where a random image of the portfolio entry should be displayed). 
      I have no clue to achieve this. May someone could give me an advice.
      Thank you in advance!
    • By ngrmm
      I have a page with a table. Each table row has a page-reference field and a checkbox.
      The Page sends emails to all users (page-refrence->email-field) and change the value of the checkbox in a row to 1.
      It works with this:
      <?php // event ID fron url query $eventID = $input->get('eventID','int'); // get event-page $event = $pages->get($eventID); // config $fromEmail = $event->event_mail_from; $fromName = $event->event_mail_from_name; $emailSubject = $event->event_subject; // email html body ob_start(); include('./_inc/emailbody.inc'); $emailBody = ob_get_clean(); // make event-page editable $event->of(false); // loop through table and send out emails foreach($event->event_clients_list as $event_table_row) { // get client page $clientPage = $event_table_row->client_name; // get client email $clientEmail = $clientPage->email; // if client isn't invited yet (checkbox not checked) if($event_table_row->client_invited == '') { // send email $m = new WireMail(); $m->to($clientEmail); $m->from($fromEmail, $fromName); $m->subject($emailSubject); $m->bodyHTML($emailBody); $m->send(); // mark client as invited $event_table_row->client_invited = 1; $event->save('event_clients_list'); } } ?> But i have to use a variable in my emailbody.inc which i'm able to get in the table-loop.
      So i do the including of the body inside my loop. But this doesn't work anymore. Page sends out the emails but is unable to change the value of the checkbox.
      I get no errors!
      I'm using ProTable
      <?php // event ID fron url query $eventID = $input->get('eventID','int'); // get event-page $event = $pages->get($eventID); // config $fromEmail = $event->event_mail_from; $fromName = $event->event_mail_from_name; $emailSubject = $event->event_subject; // loop through table and send out emails foreach($event->event_clients_list as $event_table_row) { // get client page $clientPage = $event_table_row->client_name; // get client email $clientEmail = $clientPage->email; // email html body ob_start(); include('./_inc/emailbody.inc'); $emailBody = ob_get_clean(); // make event-page editable $event->of(false); // if client isn't invited yet (checkbox not checked) if($event_table_row->client_invited == '') { // send email $m = new WireMail(); $m->to($clientEmail); $m->from($fromEmail, $fromName); $m->subject($emailSubject); $m->bodyHTML($emailBody); $m->send(); // mark client as invited $event_table_row->client_invited = 1; $event->save('event_clients_list'); } } ?>  
    • By flydev 👊🏻
      Bootstrap-4 Minimal site profile for ProcessWire
      This profile is based on the "minimal site profile (intermediate edition)" and bundled with Boostrap v4.4.1
       
        Features
      Bootstrap SASS Font-Awesome SASS Render / helper functions for : Simple ul navigation Bootstrap Multi-level navbar Bootstrap Carousel Bootstrap Cards Bootstrap Jumbotron Boostrap Accordion Assets minification, files bundle   Dependencies
      jQuery Popper.js Bootstrap FontAwesome   Prequisites
       You'll want to install the following on your system before proceeding:
      Yarn / NPM How To Install
      Download the zip file at Github or clone directly the repo with git clone and skip the step 2. Extract the folder site-pwbs4-master into a fresh ProcessWire installation root folder. During the installation of ProcessWire, choose the profile "ProcessWire Bootstrap 4 profile".   After installation
      You can find the development file (CSS/SCSS/JS) in site/assets/dev/src
      The profile can be used as is only with $config->debug set to false. To use it in debug mode, you are required to install the dependencies with the package manager.
      Open a terminal in site/assets/dev and execute the following command-line: yarn Available commands :
      Rebuild, minify and bundle assets for release : yarn build   References
      Bootstrap v4 documentation ProcessWire documentation ProcessWire Forum: bootstrap tag ProcessWire Forum: bootstrap related posts   Credits
      The ProcessWire staff Inspiration from @gebeer and his Bootstrap 3 profile post Members who contributed in various post about Bootstrap navigation and code (see code-source for refs). Screenshots

    • By Vigilante
      In general, is there an easy way to know which method should be used to access the API?
      For example, when _ini.php is used in the theme, it would seem you have to use wire()->addHookBefore(...). But on other sites where they used ready.php, I've seen it go straight to just doing $this->addHookAfter() even though there is no class or namespace set up in the ready.php file.
      So how do I know when I can do $this, or $wire, or wire() to access things? And any other variables I'm not aware of.
      Thanks!
×
×
  • Create New...