• Vanilla on Rails: The Coexistence of PHP and Ruby

    Dec 4 2006

    I'm going to debunk another myth that might keep you from trying out Ruby on Rails (or any other new server language). MYTH: Once you start using Rails, you have to do everything in Rails.

    I wanted to integrate a forum into my new Rails site. So I took a look at the Rails forums out there and found a whopping three: Beast, RForum and Opinion. Unfortunately, they all suck. Ok, to be fair, they're all rather new, and are still in development. But they still suck.

    At first, I was okay with using a crappy forum. But I didn't just need a standalone forum — I needed to totally integrate the forum into my existing site, users and all. But rails apps want to run on their own server. I don't know why, they're all claustrophobic or something. When you try to get two of them to share a server, one tends to peck the other's eyes out like it's a rails-powered cockfight.

    So after way too much time spent cleaning up feathers and blood, I decided to give one of the classic PHP forums a chance. I decided Vanilla was going to be my forum of choice, and I got to work.

    It turned out that it took me less time to integrate Vanilla with my Rails app than all the time I spent recoding routes.rb to make Beast work. Here's some tips to get a PHP app to coexist with rails:

    1. Install the forum inside /public/

      PHP apps are very happy to live inside a subdirectory inside another site. They love it. So what better place to drop one than inside a rails app's public directory. (Try doing that with a rails forum).

      This also has the benefit of having a single domain, which makes sharing cookies slightly easier.

    2. Let the rails app and php forum share a database

      This isn't a requirement, but it just simplifies things slightly. Vanilla uses a table naming convention like LMU_*, and I'm sure most other (non-rails) forums do the same, so the two can coexist fairly easily. This way you can do stuff like joins across your own tables and the forum tables.

    3. Manipulate the forum tables from Rails

      Rather than share a single 'users' table (which is pretty much the only way you'd be able to get two rails apps to coexist in a single database), just add rows to the forum's user table every time a user signs up on your rails app. And of course, don't forget to use a foreign key to relate the two.

      Vanilla makes a single sign-on feature easy through a "Remember me" feature. Basically, if 2 cookies are set ('lussumocookieone' for the UserID, and 'lussumocookietwo' for the VerificationKey by default), then the user is automatically logged in. So all you have to do is look up (or write) the VerificationKey in LMU_Users and set these cookies whenever a user logs in to your rails app.

    4. Delete PHP's cookies to destroy a PHP session

      This is a really easy hack. Just erase the 'PHPSESSID' and '_session_id' cookies, and the user will be logged out from the forum. This way you can have a single sign-out too.

    Okay, this post got pretty specific. But the lessons can be applied to the coexistence of any server-side apps. Communicate via the database and cookies, and you can pretty much do anything.

  • Comments

    1. Landon at 10:16am on December 17, 2006

    I've been searching for a way to integrate the Vanilla forum into a rails app I'm developing.  Before finding your article I tried dumping Vanilla into the /public folder.  However, I get the error:

    Routing Error

    Recognition failed for "/forum/"

    I'm doing everything locally for now, but I'm not sure how to proceed to get Vanilla installed.

    I'm using InstantRails so I definitely have Apache, MySQL going.  Any ideas?

    2. Jesse Skinner at 11:22am on December 17, 2006

    Perhaps your web server doesn't support PHP? I get the same error when I try it on my WEBrick server. However, it works when I try it through XAMPP.

    Try installing XAMPP and see if it works for you then. Or, try setting it up live and see if that makes a difference.

    3. Jason Barnabe at 11:39pm on December 21, 2006

    Thanks for the info, I was trying to get Beast going but after reading these instructions I found going to a PHP forum to be much easier. Do you have any tips on hacking Vanilla itself, for example to prevent users from registering directly in the forum and instead go through the main site?

    4. Jesse Skinner at 1:37am on December 22, 2006

    Jason - I created a theme, which lets you change any of the display files you like, then I made a copy of menu.php and added an this if statement inside the navigation loop:

    if ($Tab['Text'] != 'Account')

    This hides the Account tab, preventing people from changing their password, etc. through the forum. I also hid any links dealing with registration or signing in with the theme.

    There are also some configuration settings you can set that will send people to your main site for logging in and out, like SAFE_REDIRECT, SIGNIN_URL and SIGNOUT_URL. Have a look in /appg/settings.php to see all the configuration options available.

    Hope that helps. All the best of luck!

    5. rick at 5:53pm on December 25, 2006

    You can integrate with a rails app exactly the same way.  It won't live in the public dir of your app, but in its own capistrano-managed directory.  I wrote a little app that used Typo's user db (even in a separate db).  It's all hackish, but it works.

    6. kramer Mike at 12:18pm on January 19, 2007

    I installed the latest version of instantrails.

    the installer installed Instanrilas, as well as thefoloowing components: MySql; phpMyAdmin, Apache; PHP.

    I plcaced PHP code in one of the "rthml" VIEW files, but the code is not rendered ( completely ignored). How can I execute php in a ruby file.


    Thanks

    7. Jason Barnabe at 10:31pm on January 28, 2007

    I've written about my experiences doing this.
    http://blog.userstyles.org/2007/01/22/vanilla-rails-single-sign-in-goodness/

    8. sathishkumar at 7:15am on March 13, 2007

    i do want know more details abt the cookies funtionalities in rails.
    Funtionalities like,
    How to set the Cookies ,
    How to delete the cookies,
    How to configure the cookies not the example like,
            set cookie=
                    cookies[:customer]="nancy"
            access cookie=
                    cookies[:customer]
    i don't want like this accessible cookies
    i do want check whether cookies is present in this machine or not?
    another one thing actually im having lot of customers. how can i check all cutomer is there.
    clear my doubt please
    otherwise send me some article or ebooks related to this topic

    9. Sean at 8:59pm on March 17, 2007

    I've been trying to get this to work, but there's a routing error, just like Landon in comment #1.  mydomain.com/forum and mydomain.com/forum/ end up with a 404, but mydomain.com/forum/index.php works ok.

    I'm trying to get a RewriteRule to fix this, but I'm wondering if you had a similar issue, and, if so, how did you fix it?  Everything is in public/forum/.

    Thanks!

    10. Sean at 9:18pm on March 17, 2007

    Doh!  I got it.  The RewriteRule was below the Rule for finding cached pages.  I moved it up and all is well now.

    11. Chi at 12:23pm on April 20, 2007

    Hey man, just read with interest your article. Im currently trying to have a single sign on with Vanilla and my website(developed with PHP). Just wondering if you could give me some pointers.

    Commenting is now closed. Come find me on Twitter.