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 louisstephens
      I have a repeater with a text field where I would like to use hanna code (to make it easier on the user) so the user can define the action of a image that is in the repeater. For example:
      1. The image should have an external link:
      - the user can just type in the url
      2. The image, when clicked, should be printable (using print.js)
      - the user can just type [[print_image]]
      That works just fine, but I am running into an "issue". The code I need to use to print is:
      onclick="printJS({printable: 'path_to_image', type: 'image'});" I can put that into the hanna code as plain text, but I have no idea how to get the image from the repeater item and pass to "path_to_image". Is this even possible?
    • By louisstephens
      I have a script that is pulling in a json feed (will be attached to a cron job later) which looks like:
      $http = new WireHttp(); // Get the contents of a URL $response = $http->get("feed_url"); if($response !== false) { $decodedFeed = json_decode($response); } Everything there works well and I can pull the id, title, status (updated, new, sold) and other items from the decoded feed in a foreach loop. My whole goal is to create pages from the feed, but if the page has already been created, with all the same exact items from the json feed, I will need to "skip" over it.
      So far, I am running into a roadblock with my checks. I guess I need to compare the json to all my pages and their values and:
      1. If an id already exists, check to see if a fields data has been updated and then update the page,
      2. If an id exists and all fields are unchanged, skip adding that page
       
      $http = new WireHttp(); // Get the contents of a URL $response = $http->get("feed_url"); if($response !== false) { $decodedFeed = json_decode($response); foreach($decodedFeed as $feed) { $u = new Page(); $u->template = $templates->get("basic-page"); $u->parent = $pages->get("/development/"); $u->name = $feed->title = $feed->id; $u->title = $feed->title; $u->status = $feed->status $u->body = $feed->title; $u->save(); $u->setOutputFormatting(false); } } else { echo "HTTP request failed: " . $http->getError(); } I am really just hung up on how to do the current page checks and matching them with the json field data.
    • By sam-vital
      Hi,
      I'm trying to setup up ProceesWire for a site I am working on, and I'm having troubles with the Database section.
      I get these errors :
      Database connection information did not work.
       SQLSTATE[HY000] [1045] Access denied for user
      I also cannot connect to phpMyAdmin, Xampp is what I am using as my stack.
      None of these errors have occurred before which is annoying me to say the least.
      Help is appreciated 🙂
    • By louisstephens
      So I was going to build a todo tracking app for myself to test/broaden my knowledge of processwire, and so far it has been taxing 😓.  My Site structure is:
      - Project One - Phase One - Task - Task - Task - Phase Two - Task - Task - Task So far it was pretty easy, I can easily foreach through the Project and get the phases with their tasks. However, it gets a bit muddled when I have more than one project as I was trying to have a dashboard where the content switches out to the selected project as opposed to accessing each project via their own url. How would yall handle this?
      My next hurdle is each task has a select field (for project status) that I want to update via ajax (for the smooth transitioning).
      Scenario: You finish a task, change the option from "new" to "completed", and an onclick changes the status drop down background to green (which I have working), but then posts/saves a field on the backend to the new option.
       
      I have a page called "Actions" set up with url segments using
      if ($config->ajax && $input->urlSegment1 == 'change-status') { //save update field on admin } However, I am a little fuzzy on how to actually pass the current page along with the new selected status to actually update the page (task). I guess I am not very far into my endeavor. Any help would be greatly appreciated.
    • By louisstephens
      So this is more or less just a question regarding best practices, and how to actually achieve this. I have an internal tool I built awhile ago that has just gotten very "trashed". The naming conventions are all out of whack due to trying to get something completed, and pages are just everywhere. The fields got really out of hand as well, but I believe I have found a much cleaner field structure with the Repeater Matrix and a few other ProFields.  I decided recently to rewrite the template structure and the fields from scratch to keep everything nice and neat, but then it hit me that I would need to move all the previous data over and populate the new fields etc.
      The current tree looks something like:
      Group Title - Section A (template "section") - Category One (template "category") - Category Two (template "category") - Section B (template "section") - Category One (template "category") - Category Two (template "category") Etc etc... The structure is pretty good, and I am thinking of keeping it as it serves my purpose. However, I now have about 140 Sections with child pages that I would hopefully like to move to different templates, as well as changing the child templates/mapping data to the new fields. The only fields I need to worry about are a few textareas, text fields, options field,  as well as 1 image filed.
      I guess my real question is, what is the best way to go about this "migration" via the api while keeping the parent/child structure. It would be ok to leave it as is and chip away at it if I were the only one using it, but unfortunately that is not the case. 
      I guess one approach would be to copy over the entire structure and make the move to the new templates/fields. Once we have verified all the data/templates are correct, we could remove the old structure and possibly rename the new to mirror the old. However, this might just be too complicated.