SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ruby on Rails -> PHP?

    I've been checking out Ruby and Rails the past few days and I'm totally impressed. Does anyone know of any PHP project that is like Rails? What about a PHP version of the ActiveRecord? Does it seem possible to re-create Rails with PHP?

    - matt

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have been maintianing an "interested watch" status on rails. My biggest problem is that the code I write tends to interface with legacy data schemas on Oracle databases, neither of which is a sweet spot for rails
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just curious, what particular features do all of you like about Ruby of Rails?

  4. #4
    SitePoint Guru
    Join Date
    Nov 2002
    Posts
    841
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've been peeking at Ruby on Rails as well. I haven't gotten an example up and running, but I've read all the tutorials, some API docs, and a host of blog posts.

    My assessment is that Rails is an MVC framework with distinct M, V, and C layers that uses introspection and a set of naming conventions to guess the proper wiring between components.

    One of the drawbacks to MVC, for example, is that if you add a field to a table, then you have to update your program in at least three places: The M layer, the V layer and the C layer. The strength of Rails is that through introspection and naming conventions, it can automatically wire these three layers together, so you only have to make a change in one place.

    The weakness of Rails, as Jason points out, is that if your three layers are not naturally parallel, then Rails won't know how to automatically wire them together and might even become a hinderance. The degree of freedom for the Model, View, and Controller layers to vary independently from each other in structure does not appear to be great under rails.

    WACT and Rails are similar in many ways. WACT tries to provide support for all three layers of MVC, as does Rails. This allows for a greater degree of integration. I think it would be hard to do what Rails does without this integration between components.

    I see many similarities between WACT and Rails at the controller level. In one of the earlier iterations of the WACT Controller, I had checked in a CRUD example that showed how to use parameterized controllers to perform CRUD operations. Unfortunately, I felt that people were confused by this and I removed it to make the example simpler. The example had only one table and the value of this approach is not clear unless you have many. However, I still use this approach in my own code.

    For example, instead of creating a AddArticleController and a AddCategoryController, I would create an AddController with a parameter which might be 'Article' or 'Category.' It is possible to eliminate an amazing amount of code duplication in the controller layer with this technique. The WACT controller architecture is designed to support this capability.

    Because of its controller layer, I think WACT can fairly easily do many of the things that Rails does. What does WACT lack right now for supporting a Rails apporach?

    WACT does not have strong introspection support in the Model layer. The DB abstraction classes do not support examining table structures and the (young) ORM layer has little introspection capability at all. There is someone working on this, but they recently started a new job and their time is limited.

    In the View layer, Jason has already written an introspection based component for displaying tabular data (The <data:table> tag). We need an equivalent for forms (<data:form>). Both of these tags will benefit from extra metadata that could be provided by an improved and explicit introspection capability.

    One of the things I would like to see for WACT is the capability to define a Domain Object and then use the parameterized controller concept with introspection in order to form a CRUD application for that object.

    So, for example:
    PHP Code:
    /*
    * @orm.table article
    * @orm.primary_key ArticleId integer auto_increment
    */
    class Article extends DomainObject {

        
    /*
        * @orm.column title char(80)
        * @orm.description Article Title
        */
        
    var $Title;
        
    /*
        * @orm.column body text
        * @orm.description Article Body
        */
        
    var $Body;
    }

    $controller =& new CrudController('Article');
    $controller->start(); 
    I want it to be almost this simple for basic crud. So in this example the CrudController is responsible for creating sub controllers for each of the pages that one would see in a CrudApplication. Say:

    CrudIndexController()
    CrudDetailController()
    CrudAddController()
    CrudDeleteController()
    CrudEditController()

    Note that the basis for introspection in this example is the domain object, not a table schema as it would be in Rails. Also, you don't have to generate a bunch of skeleton files.

    The hierarchical composability of WACT controllers would allow you to override portions that you want to change.
    PHP Code:
    $controller =& new CrudController('Article');
    $controller->addChild('index', new MyCustomIndexController());
    $controller->start(); 
    This is much the same approach that Rails takes. I've been working for a long time to allow even greater degrees of independence between the M, V, and C layers in WACT. I predict that this is exactly what Rails will try to do in future releases. For example, moving away from active record towards mapping.

    Anyway, I am very much keeping an eye on Rails.

  5. #5
    SitePoint Zealot
    Join Date
    Aug 2002
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know I should't be talking about this before anything is released, but....

    If you are interested in ActiveRecord: keep an eye out on my blog in 3-4 weeks, inspecting AR with PDO - full introspection and support for Postgres, Mysql and Sqlite so far ( no: nothing released yet, I'm keeping this until I get a chance to develop a proper application with it first )

    A quick teaser
    PHP Code:
        <?php
        
    include( "settings.php" );
        
       
    // Table: notes
        
    class Notes extends ActiveRecord{ }
        
       
    // Table: user
        
    class User extends ActiveRecord
        
    {
            public function 
    __construct()
            {
                
    parent::__construct();
               
    // Each user has many Notes attatched to him/her
                
    $this->hasMany"notes:user" );
            } 
        }
       
    // Default we say that the primary key is 'id', but 
       // we can change this at run time before using the object
        
    User::primaryKey'uid' );
        
        
    $user = new User();
        
    $me $user->find);
        echo 
    "Hello, my name is: " $me->username "<br />";
        echo 
    "<ul>";
        foreach( 
    $me->notes as $comment )
        {
            echo 
    "<li>" $comment->id .  "::" $comment->title "<br />";
            echo 
    $comment->note "</li>";
        }
        echo 
    "</ul>";
        
    ?>
    Everything is done with introspection ( no configuration at all, just a couple of rules that needs to be enforced - but these can be changed at runtime ) and a whole lot of __get/__set/__call functionality. Caching of the metadata is also done to ensure better performance. I will get back to this later on, just thought I would give a note about this if anybody needs this functionality and don't want to start on it.

  6. #6
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    I'd be interested from the point of view of PDO

    Consider your blog bookmarked btw.

  7. #7
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, what the Dr. said!

    -m

  8. #8
    SitePoint Enthusiast Ilija Studen's Avatar
    Join Date
    Oct 2003
    Location
    Serbia
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, bloglined
    activeCollab
    Project management and collaboration tool. On your server.

  9. #9
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Porting ActionPack might be quite simple. The core is the routing. A set of routes are stored in an array:
    PHP Code:
    $routes = array(
        
    "/archive/{date}/{action}/ | {controller => archive}",
        
    "/{controller}/{action}/{id}/"
    ); 
    Which is then compared with the query string from a .htaccess file's mod_rewrite:
    PHP Code:
    $query_string "/products/list/"
    To give an associative array:
    PHP Code:
    $route = array(
        
    "controller" => "products",
        
    "action" => "list"
    ); 
    (Look at the Rails source for the logic behind this, should be easy to port.)

    The next step is to call the controller. To isolate the controller from the view (which is just going to be a plain PHP template) we want to isolate the data output by the controller from the controller itself. Take this code:

    PHP Code:
    class Alpha {
        function 
    store($data) {
            
    $this->data $data;
        }
    }
    class 
    Beta{
        function 
    Beta($data) {
            
    Alpha::store($data);
        }
    }

    $b = new Beta('Hello!');
    echo 
    $b->data
    Clever, isn't it? We can store the value saved in Alpha inside the variable which points to the Beta class! Translating this into our problem:

    PHP Code:
    class Runner {
        function 
    Runner($controller_name$action) {
            
    $this->template $action// more on this line later
            
    eval("$controller_name::$action();");
        }

    Where the controller looks like this:

    PHP Code:
    class Products {

        function list() {
            
    $this->products Products.find_all// This could be a call to PDO
        
    }


    Now we have a Runner class with $this->products set. Pass the Runner as a $data object to our template:

    PHP Code:
    // list.phtml
    <table>
    <?php foreach($data->products as $product) { ?>
        <tr><td><?php echo $product->name ?></td></tr>
    <?php ?>
    </table>
    And a simple template calss (replace with SimpleT or similar to get caching and so on)

    PHP Code:
    class Template {
        
        function 
    render($data$template_file) {
            include(
    $template_file);
        }
        

    And all that is left is the code to call it all:

    PHP Code:
    $data = new Runner($route['controller'], $route['action']);

    $t = new Template();
    $template_file $data->template ".phtml";
    $t->render($data$template_file); 
    Things in ActionPack like session handling can easily be added as there is support built into PHP. Other things like pre-filters need to be added, but theabove makes up the core of ActionPack, giving you the VC layers. The M layer could be PDO or Propel or another PHP ORM.

    What do you all think?

    Douglas
    Hello World

  10. #10
    As the name suggests... trickie's Avatar
    Join Date
    Jul 2002
    Location
    Melbourne, Australia
    Posts
    678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am working on the ActionPack implementation.

    It is little more detailed than that, but you are right, it is not that difficult to port. Alot of the cool features in ActionPack use Ruby specific things, but most of it has an equivelant implementation in PHP.

    The interesting thing is going to be trying to get the debugging and breakpoint functionality ported. I am looking into if Xdebug will work with this.

    I like your simplified routing idea, i think i have been overcomplicating it actually

  11. #11
    SitePoint Evangelist
    Join Date
    Jun 2003
    Location
    Melbourne, Australia
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Others are liking Ruby on Rails' ideas too.
    Zealotry is contingent upon 100 posts and addiction 200?

  12. #12
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, not sure just how revalant this link is, but I came across it and seeing as we have had in the past, discussions on Hibernate and now Ruby on Rails, I thought I'd post it, Hibernate Vs Rails: The Persistence Showdown,

    http://www.theserverside.com/article...RailsHibernate

    Not had a read of it so please excuse me...

  13. #13
    SitePoint Member
    Join Date
    Apr 2005
    Location
    Atlanta, GA
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try CAKE.

    Cake is a framework for PHP, based on Rails.
    Later,

    oaker
    www.oaker.com | Great Hosting: www.razorhost.net

  14. #14
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Hanoi
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The same, Solar ( http://www.solarphp.com )

  15. #15
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    Montreal
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about we port Rails to PHP5
    Call it "POW : PHP on Wings"

  16. #16
    SitePoint Member
    Join Date
    Jan 2004
    Location
    Continente de São Pedro
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sweatje
    I have been maintianing an "interested watch" status on rails. My biggest problem is that the code I write tends to interface with legacy data schemas on Oracle databases, neither of which is a sweet spot for rails
    Have you tried ObectGraph ... it says to support Oracle!

  17. #17
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dokkalfr
    Have you tried ObectGraph ... it says to support Oracle!
    I have not had a serious look at it yet, but it is on my todo list.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •