SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 46 of 46
  1. #26
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ren
    Interesting, have you had the need use this functionality?
    The same result can be achieved by being more explicit in the url-generating calls, i.e. supplying null values for the parameters after the one that was changed, but having this functionality built in makes things a fair bit simpler. In the end though, I didn't even consider if I would need it when I implemented it; I just made a bunch of test cases based on the behavior of the rails route class and hacked away until it all worked.

  2. #27
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After some sunday hacking, added named wildcard support, turned out fairly easy.

    Also included a simple example dealing with dates.

    PHP Code:
            function testWildcardWithDefault()
            {
                
    $route = new Route('userblog''*url/:username',
                        array(
    'controller' => 'blog''action' => 'view''username' => 'george'));
                
                
    /*
                    Due to the default username, the whole path is treated as part of the wildcard.
                */
                
    $url = new Url('some/long/url/george');
                
    $this->assertEqual($route->isMatch($url),
                                        array(
    'controller' => 'blog''action' => 'view''username' => 'george',
                                                    
    'url' => 'some/long/url/george'));
                
                
    $url = new Url('some/other/stuff/fred');
                
    $this->assertEqual($route->isMatch($url),
                                    array(
    'controller' => 'blog''action' => 'view''username' => 'george',
                                            
    'url' => 'some/other/stuff/fred'));    
            }
            
            function 
    testWildcardWithoutDefault()
            {
                
    $route = new Route('userblog''*url/:username',
                            array(
    'controller' => 'blog''action' => 'view'));
                
    /*
                    Because the username now doesn't have a default, it is always extracted from path.
                */
                
    $url = new Url('some/long/url/george');
                
    $this->assertEqual($route->isMatch($url),
                        array(
    'controller' => 'blog''action' => 'view'
                                
    'username' => 'george',
                                
    'url' => 'some/long/url'));

                
    $url = new Url('some/other/stuff/fred');
                
    $this->assertEqual($route->isMatch($url),
                                    array(
    'controller' => 'blog''action' => 'view''username' => 'fred',
                                            
    'url' => 'some/other/stuff'));    
            } 
    http://routes.groovie.org/manual.html is pretty informative.
    Attached Files Attached Files
    Last edited by Ren; Feb 19, 2006 at 18:40. Reason: Slight bug squashed.

  3. #28
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ren
    After some sunday hacking, added named wildcard support, turned out fairly easy.

    Also included a simple example dealing with dates.
    Definitely interesting stuff. Thanks a lot. All hail Ren!

  4. #29
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, not had a look at the ZIP yet, I hope to find some time this week, but I agree that this is very interesting, and hope to gleam something from it myself, now...

    As for hailing Ren, I'm a God so I'm just as about the highest being that there is possible? Surely there can't be anyone who is above a God?

  5. #30
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    As for hailing Ren, I'm a God so I'm just as about the highest being that there is possible? Surely there can't be anyone who is above a God?
    Well, there's always the Composite design pattern.

  6. #31
    SitePoint Zealot
    Join Date
    Sep 2005
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm,

    hahaha!

  7. #32
    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 Dr Livingston
    I'm a God
    Who deified you?
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  8. #33
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sweatje
    Who deified you?
    SitePoint? If I'm not mistaken I noticed a deity of the programming kind posting here regularly

  9. #34
    SitePoint Guru thr's Avatar
    Join Date
    Jun 2003
    Location
    Sweden
    Posts
    664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ren: Looked thru your code, nice work . About the whole route thing tho - great idea but won't it leave to kinda duplicated code? If I need to create an instance on every page I wanna generate an URL to the news archive and then call the kinda bulky route constructor with the correct params to get my route object/link/url - and how does the generated URLs look(Is some type of mod_rewrite required?) ?

  10. #35
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by thr
    Ren: Looked thru your code, nice work . About the whole route thing tho - great idea but won't it leave to kinda duplicated code? If I need to create an instance on every page I wanna generate an URL to the news archive and then call the kinda bulky route constructor with the correct params to get my route object/link/url - and how does the generated URLs look(Is some type of mod_rewrite required?) ?
    mod_rewrite is preferred, the other method is to register the front controller page as a 404 error handler.

  11. #36
    SitePoint Guru thr's Avatar
    Join Date
    Jun 2003
    Location
    Sweden
    Posts
    664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, ah the 404-hack - if you rewrite the header sent it's perfectly viable tho :]. The code looks realy sleak, I'll give the route thingy a shot later tonight maybee and hack some myself - realy nice work tho

  12. #37
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ren
    the other method is to register the front controller page as a 404 error handler.
    This will probably pollute your webserver's access logs...

    Just had a look at your tests. What if you want to distinguish a wildcard from a static component? I can imagine routes with mixed dynamic and static components like /static/:dynamic:/another_static/:another_dynamic.

  13. #38
    SitePoint Guru thr's Avatar
    Join Date
    Jun 2003
    Location
    Sweden
    Posts
    664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by michel
    This will probably pollute your webserver's access logs...
    There are ways around it tho ;p start by sending the 200 header instead of 404 to the client so searchengines don't ignore you and the accesslog is possible to fix also (or just write your own accesslog)

  14. #39
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by thr
    There are ways around it tho ;p start by sending the 200 header instead of 404 to the client so searchengines don't ignore you and the accesslog is possible to fix also (or just write your own accesslog)
    Sure, but who would want to go through the extra trouble when there are dependable tools?

  15. #40
    SitePoint Guru thr's Avatar
    Join Date
    Jun 2003
    Location
    Sweden
    Posts
    664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    michel: All hosts don't give you the ability to install mod_rewrite, etc. - I just made a remark that it's possible to work around - now let's stay on topic as this is realy intereseting.

  16. #41
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by michel
    This will probably pollute your webserver's access logs...

    Just had a look at your tests. What if you want to distinguish a wildcard from a static component? I can imagine routes with mixed dynamic and static components like /static/:dynamic:/another_static/:another_dynamic.
    Well the 404 is there for when caching directly to the public web directory, would only get a 404 in the logs for cache misses. See livedocs.


    PHP Code:
    function testTwoDynamicsSeperatedByStatic()
            {    
                
    $route = new Route('a''/static/:dynamic/another_static/:another_dynamic');
                
                
    $url = new Url('/static/first_dynamic/another_static/second_dynamic');
                            
                
    $this->assertEqual($route->isMatch($url), array('dynamic' => 'first_dynamic',
                                                                    
    'another_dynamic' => 'second_dynamic'));
            } 
    Works.

  17. #42
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ren
    PHP Code:
       function testTwoDynamicsSeperatedByStatic()
               {    
                   
    $route = new Route('a''/static/:dynamic/another_static/:another_dynamic');
                   
                   
    $url = new Url('/static/first_dynamic/another_static/second_dynamic');
                               
                   
    $this->assertEqual($route->isMatch($url), array('dynamic' => 'first_dynamic',
                                                                       
    'another_dynamic' => 'second_dynamic'));
               } 
    Works.
    Cool. I was wondering about url creation, but this passes as well:
    PHP Code:
        function testUrlCreationWithMixedDynamicsAndStatics()
        {
            
    $route = new Route('test''/static/:dynamic/another_static/:another_dynamic');
            
    $url $route->createUrl(array('dynamic' => 'one''another_dynamic' => 'two'));
            
    $this->assertEqual('/static/one/another_static/two'$url->toString());
        } 
    Off Topic:

    Quote Originally Posted by thr
    michel: All hosts don't give you the ability to install mod_rewrite, etc. - I just made a remark that it's possible to work around - now let's stay on topic as this is realy intereseting.
    Aye, aye, sir Nevertheless an important aspect to consider when choosing a technique.

  18. #43
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by michel
    Cool. I was wondering about url creation, but this passes as well:
    [php]
    function testUrlCreationWithMixedDynamicsAndStatics()
    {
    $route = new Route('test', '/static/:dynamic/another_static/:another_dynamic');
    $url = $route->createUrl(array('dynamic' => 'one', 'another_dynamic' => 'two'));
    $this->assertEqual('/static/one/another_static/two', $url->toString());
    }
    Yeha, the preg_split in getParts() creates an array that looks like

    (
    0 => '/static'
    1 => ':'
    2 => 'dynamic'
    3 => '/another_static/'
    4 => ':'
    5 => 'another_dynamic'
    6 => ''
    )

    Which is a repeating pattern, static bit, type of match (dynamic (:) or wildcard (*) ), and the name of wildcard/dynamic. Just matter of pieceing it back together replacing the type&name with something else.

  19. #44
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 33degrees
    Going back to the topic; in rails, on top of the validation done to urls (which prevents the controller from trying to work with invalid data), the business logic validation is done in the models. You can set up a bunch of rules to validate against when you insert a record, update a record, or both; If the validation fails, the save() call returns false, and you can query the Errors object of the model to find out what went wrong. I've yet to come across a situation where this wasn't enough.
    Just started reading Rails source, and the ActiveRecord validation is kind of interesting, thou it does seem abit odd in places. Namely the confirmation (entering email twice), as that validation is done inside AR, which seems a slight abuse on the pattern.

  20. #45
    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 Ren
    Just started reading Rails source, and the ActiveRecord validation is kind of interesting, thou it does seem abit odd in places. Namely the confirmation (entering email twice), as that validation is done inside AR, which seems a slight abuse on the pattern.
    I guess technically the validation is done as part of the Model, which extends the ActiveRecord pattern for persistance. From that perspective, it makes more sense why it is included there.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  21. #46
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sweatje
    I guess technically the validation is done as part of the Model, which extends the ActiveRecord pattern for persistance. From that perspective, it makes more sense why it is included there.
    Aye, its just they treat the 2nd copy of the email/password as a virtual column on the AR object.

    # The person has to already have a password attribute (a column in the people table), but the password_confirmation is virtual.
    # It exists only as an in-memory variable for validating the password. This check is performed only if password_confirmation
    # is not nil and by default on save.
    from rails\vendor\rails\activerecord\lib\active_record\validations.rb


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
  •