Hi, As I''m progressing through my learning project (a simple forum), suddenly I realized most of the time I''d like to use it inside another website (Which may be Rails, but could be PHP, for example), so I was thinking how could you do this? I was thinking that I could use an HTTP client to retrieve the contents that my app outputs and write them inside the website. But that''s too much overhead me thinks. Would it be possible to call the Rails app directly, and perhaps ommit it from rendering the layout (only the controllers views)? Or what other approach could there be? Thanks in advance. - Ivan Vega R.
On Oct 18, 2005, at 9:29 PM, Iván Vega Rivera wrote:> Would it be possible to call the Rails app directly, and perhaps > ommit it from rendering the layout (only the controllers views)?I haven''t tested this, but.... Let''s say you have /path/to/docroot that contains a pile of steaming PHP. Now let''s say you put your rails app at /path/to/ rails_goodness. Set up apache with an Alias for /rails to /path/to/ rails_goodness. Now http://my.hostname.dom/foo will still be the steaming PHP, but http://my.hostname.dom/rails will have the rails goodness. This isn''t integrated as much though. You still have to duplicate the layout because rails is rendering the entire page. I''m not sure the best way to handle this, but you can always call render from your controllers with :layout => false. If you only wanted to render a portion of the page, you _could_ make an http call to http://my.hostname.dom/rails and then just have all your controllers use :layout => false. However, doing this essentially would make all your PHP pages act as the controller. They''d have to duplicate the logic of knowing which URL to call to get the right content. So if you make Rails standalone along side your PHP then you duplicate your site design and layout. If you make your PHP call into Rails you duplicate the business logic of knowing what page belongs where. Personally, I''d duplicate the site design and make Rails standalone. ----- Doug Alcorn - http://lathi.net/RubyOnRailsDeveloper doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
I''m working on a similar project, and what I have come up with is this (with some help from this list, and IRC: Rails is running as a regular install, with the original project symlinked into the /public dir (I use lndir as part of the deployment process.) Lighttpd is configured with 4 fcgi servers, kinda like this: <pre> server.document-root = var.appdir + "/" server.indexfiles = var.appdir + "/dispatch.fcgi" server.error-handler-404 = "/dispatch.fcgi" ... fastcgi.server = ( ".fcgi" => ( "rails" => ( "socket" => "/tmp/" + var.username + "-" + var.appname + "-fastcgi.socket", "bin-path" => var.appdir + "/dispatch.fcgi", "bin-environment" => ("RAILS_ENV" => "development") ) ), ".php" => ( "php-app" => ( "socket" => "/tmp/" + var.username + "-" + var.appname + "-php.socket", "bin-path" => ''/usr/bin/php-cgi" ) ), ".html" => ( "html" => ( "socket" => "/tmp/" + var.username + "-" + var.appname + "-php.socket", "bin-path" => ''/usr/bin/php-cgi" ) ), ".cgi" => ( "cgi" => ( "socket" => "/tmp/" + var.username + "-" + var.appname + "-cgi.socket", "bin-path" => ''/usr/bin/perl" ) ) ) } </pre> All requests go through dispatch.fcgi, but if there is no route to the controller, rails looks for a file in public, and renders it normally. If the file has a .php or .html extension, it is handed off to php-cgi for rendering, likewise for perl scripts, and the system works as expected. As development proceeds, you can implement rails views for the static stuff, or replace php with rails, and if the dispatcher finds a route to a controller and can invoke a method specified in the url, rails will handle the request. On 10/18/05, Iván Vega Rivera <ivanvr-Xl95p0XkWPRBDgjK7y7TUQ@public.gmane.org> wrote:> > Hi, > > As I''m progressing through my learning project (a simple forum), > suddenly I realized most of the time I''d like to use it inside another > website (Which may be Rails, but could be PHP, for example), so I was > thinking how could you do this? > > I was thinking that I could use an HTTP client to retrieve the contents > that my app outputs and write them inside the website. But that''s too > much overhead me thinks. > > Would it be possible to call the Rails app directly, and perhaps ommit > it from rendering the layout (only the controllers views)? > > Or what other approach could there be? > > Thanks in advance. > > - Ivan Vega R. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- ********************************* All that is gold does not glitter. Not all those who wander are lost. The old who are strong do not whither. Deep roots are not touched by the frost. -- J.R.R. Tolkein lotus labs po box 682 lotus, ca 95651 leepope-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://lotuslabs.net _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Oct 19, 2005, at 6:49 AM, Doug Alcorn wrote:> > On Oct 18, 2005, at 9:29 PM, Iván Vega Rivera wrote: > > >> Would it be possible to call the Rails app directly, and perhaps >> ommit it from rendering the layout (only the controllers views)? >> > > I haven''t tested this, but.... > > Let''s say you have /path/to/docroot that contains a pile of > steaming PHP. Now let''s say you put your rails app at /path/to/ > rails_goodness. Set up apache with an Alias for /rails to /path/to/ > rails_goodness. Now http://my.hostname.dom/foo will still be the > steaming PHP, but http://my.hostname.dom/rails will have the rails > goodness. This isn''t integrated as much though. You still have to > duplicate the layout because rails is rendering the entire page. > > I''m not sure the best way to handle this, but you can always call > render from your controllers with :layout => false. If you only > wanted to render a portion of the page, you _could_ make an http > call to http://my.hostname.dom/rails and then just have all your > controllers use :layout => false. > > However, doing this essentially would make all your PHP pages act > as the controller. They''d have to duplicate the logic of knowing > which URL to call to get the right content. So if you make Rails > standalone along side your PHP then you duplicate your site design > and layout. If you make your PHP call into Rails you duplicate the > business logic of knowing what page belongs where. Personally, I''d > duplicate the site design and make Rails standalone.You could also use php''s fopen or curl functions to call a rails app and render the action without a layout. Maybe something like this: MyAppController < ApplicationController def myaction @content = MyApp.find :all, ...... if params[:context] == "no_layout" render :layout => false && return end end end # end MyAppController Now in your steaming pile php script define this functionality: <?php /* *Function uses CURL lib, follow the link to get help: http:// www.php.net/curl * @return string * @param string $url * @desc Return string content from a remote file */ function get_content($url) { $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_HEADER, 0); ob_start(); curl_exec ($ch); curl_close ($ch); $string = ob_get_contents(); ob_end_clean(); return $string; } #usage: $content = get_content ("http://www.php.net"); var_dump ($content); ?> Now you can use the following to get a view rendered from your rails app without a layout and still be able to get a layout if you are using the rails app directly from rails: <?php $content = get_content ("http://myrailsapp.com?context=no_layout"); echo $content; ?> So this will allow you to use rails components and actions rendered into your php app without the rails layouts getting in the way. Note that you can also set up lighttpd/fcgi or apache/fcgi to serve .php scripts out of your RAILS_ROOT/public directory so that you can use the same url without any fancy rewriting. If you do this and you are using lighttpd/fcgi then make sure to add "index.php" to your server-index-files command in your lighttpd.conf. And also remember that even if you are serving a .php script out of your RAILS_ROOT/public directory and you want to include a rails action in the php script like described above then you still must use the get_content(); function in php because the request for your rails action must go over http. You cannot just use fopen with a local path to your rails action and get it to render correctly. Of course this is only for the extreme case where you _must_ have php and rails stuff mixed. Or if you want to move your php app to rails but cannot do so all at once this makes a great way to set up a rails app with your php app tucked neatly inside of it and then as you build rails functionality to replace php functional;ity you can just remove the php and replace with rails actions in your own time frame. Here is the relevant section of a lighttpd.conf file that will run rails and php out of the same public dir: # Note that you want to compile php as a fastcgi binary to use this. You can also just use a php-cgi command line binary but it won''t be as fast as php-fcgi. fastcgi.server = ( ".fcgi" => ( "yhr_app" => ( "socket" => "/tmp/rails_app.socket", "bin-environment" => ( "RAILS_ENV" => "production" ), "bin-path" => "/path/to/rails/app/public/dispatch.fcgi", "min-procs" => 10, "max_procs" => 10 ) ), ".php" => ( "localhost" => ( "socket" => "/tmp/php-fcgi.socket", "bin-path" => "/usr/bin/php-fcgi", "bin-environment" => ( "PHP_FCGI_CHILDREN" => "4", "PHP_FCGI_MAX_REQUESTS" => "2000" ) ) ) ) Hope this helps you out! Cheers- -Ezra Zygmuntowicz Yakima Herald-Republic WebMaster http://yakimaherald.com 509-577-7732 ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org
Doug, Ezra, Lee, Thank you all. You provided very helpful insight. Regards, Iván V. Doug Alcorn wrote:> > On Oct 18, 2005, at 9:29 PM, Iván Vega Rivera wrote: > >> Would it be possible to call the Rails app directly, and perhaps >> ommit it from rendering the layout (only the controllers views)? > > I haven''t tested this, but.... > > Let''s say you have /path/to/docroot that contains a pile of steaming > PHP. Now let''s say you put your rails app at > /path/to/rails_goodness. Set up apache with an Alias for /rails to > /path/to/rails_goodness. Now http://my.hostname.dom/foo will still be > the steaming PHP, but http://my.hostname.dom/rails will have the rails > goodness. This isn''t integrated as much though. You still have to > duplicate the layout because rails is rendering the entire page. > > I''m not sure the best way to handle this, but you can always call > render from your controllers with :layout => false. If you only > wanted to render a portion of the page, you _could_ make an http call > to http://my.hostname.dom/rails and then just have all your > controllers use :layout => false. > > However, doing this essentially would make all your PHP pages act as > the controller. They''d have to duplicate the logic of knowing which > URL to call to get the right content. So if you make Rails standalone > along side your PHP then you duplicate your site design and layout. > If you make your PHP call into Rails you duplicate the business logic > of knowing what page belongs where. Personally, I''d duplicate the > site design and make Rails standalone. > > ----- > Doug Alcorn - http://lathi.net/RubyOnRailsDeveloper > doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >