Tagged: controller Toggle Comment Threads | Keyboard Shortcuts

  • danielsaidi 10:49 pm on October 5, 2010 Permalink | Reply
    Tags: , controller   

    A really simple PHP Controller class 

    I have written a reaaaally simple Controller class to my hobby project, Wigbi. Even though it is SO basic, it will help immensely when developing MVC-based PHP web applications.

    The class has three properties – name(…), action(…) and viewData(…) – and one single method – addView(…). I will try it out in a couple of projects and see if it is good enough.

    Here is the code:

    <?php
    class Controller
    {    
     /**#@+
     * @ignore
     */
     private static $_currentViewData;
     private static $_action = "";
     private static $_name = "";
     private static $_viewData = array();
     /**#@-*/
    
     /**
      * Get/set the name of the current action.
      *
      * @access    public
      *
      * @param    string    $value    Optional set value.
      * @return   string              The name of the current action.
      */
     public function action($value = "")
     {
        if(func_num_args() != 0)
           Controller::$_action = func_get_arg(0);
        if (Controller::$_action)
           return Controller::$_action;
        if (!array_key_exists("action", $_GET))
           return "";
        return $_GET["action"];
     }
    
     /**
      * Get/set the name of the controller.
      *
      * @access    public
      *
      * @param     string    $value    Optional set value.
      * @return    string              The name of the controller.
      */
     public function name($value = "")
     {
        if(func_num_args() != 0)
           Controller::$_name = func_get_arg(0);
        if (Controller::$_name)
           return Controller::$_name;
        if (!array_key_exists("controller", $_GET))
           return "";
        return $_GET["controller"];
     }
    
     /**
      * Get/set view data.
      *
      * @access    public
      *
      * @param        string    $key        Optional key value, otherwise current, view specific data.
      * @param        string    $value    Optional set value.
      * @return    string                     The view data value.
      */
     public function viewData($key = "", $value = "")
     {
        if (func_num_args() == 0)
           return Controller::$_currentViewData;
        if (func_num_args() == 2)
           Controller::$_viewData[$key] = $value;
        return array_key_exists($key, Controller::$_viewData)
           ? Controller::$_viewData[$key]
           : null;
     }
    
     /**
      * Add a view to the page.
      *
      * If ~/ is used in the view path, Wigbi will auto-parse it to the
      * application root path.
      *
      * @access    public
      * @static
      *
      * @param    string    $viewPath    The path to the view file.
      * @param    string    $viewData    Optional view data; default null.
      */
     public static function addView($viewPath, $viewData = null)
     {
        Controller::$_currentViewData = $viewData;
    
        $viewPath = str_replace("~/", Wigbi::serverRoot(), $viewPath);
        require $viewPath;
    
        Controller::$_currentViewData = null;
     }
    }
    ?>

    In the application root, I place an .htaccess file with the following content:

    Options -Indexes
    Options +FollowSymLinks
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^([a-zA-Z0-9_\-]+)/?$                   controllers/$1Controller.php?controller=$1                        [NC]
    RewriteRule ^([a-zA-Z0-9_\-]+)/([a-zA-Z0-9_\-]+)$   controllers/$1Controller.php?controller=$1&action=$2    [NC]
     
  • danielsaidi 9:35 pm on August 15, 2010 Permalink | Reply
    Tags: action, , controller   

    Get the name of the current controller and action in ASP.NET MVC 

    I have been Googling my a** off to find how you get the name of the current controller and action. Eventually, I found this short, great page:

    http://w3mentor.com/learn/asp-dot-net-c-sharp/c-asp-net-mvc/get-name-of-current-controller-and-action-in-mvc/

    Not that straightforward…maybe time to create a small helper 🙂

     
    • torm 4:08 pm on January 5, 2011 Permalink | Reply

      Just curious – why would you need to check current controller ?

      • danielsaidi 9:09 am on January 11, 2011 Permalink | Reply

        When I wrote this post, I used the name of the current controller and action to highlight selected menu items.

  • danielsaidi 6:54 pm on February 1, 2010 Permalink | Reply
    Tags: controller, interface builder, striped background, view   

    Set striped background in iOS 3.1 

    I’ve finally begun looking into iOS development on my spare time. It’s good fun and I will write about my experiences here, as my work progresses.

    I first started to play around with this good tutorial. After completing it, I thought I’d just replace the plain white background with the striped background that is so common in iPhone apps.

    Beautiful, striped background

    Beautiful, striped background

    However, no matter where I looked in Interface Builder, I could not find a way to set this striped background. Sure, you could insert a table view, set its style to Grouped and let it fill the window…but I want the view itself to have the background.

    After searching around, I found that many developers set the striped background with either an image (which I will not cover since I do not want to use images when not absolutely necessary) or programatically.

    Option 1 – set the striped background programatically (“bad”)

    It is really simple to set the striped background programatically. Just do the following:

       //If you have a controller with the view, do this
       theController.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
       //If you have the view directly, do this
       theView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    

    However, is this as good as it gets? This way of setting the background means that the stripes will only show when you run the application and not if you open the view in Interface Builder…and that just don’t do it for me 🙂

    Thus, I mark this option as a “bad” way of setting the default background. Naturally, you may want to change the background programatically for various reasons, but maybe not the default background.

    Option 2 – set the striped background in the .xib file (“good”)

    Well, I did not find a way to set the striped background in Interface Builder (if you do know how, please leave a comment), but it is really easy to set it by modifying the .xib file in a text editor.

    To do so, just right click the .xib file you want to modify and select “Open As > Plain Text File”. Then, in the text editor, search for the following tag:

    
    

    For a plain, white view, the section should look something like this:

       
          1
          MC45ODQ3MTEyMDQ3IDAuOTg4NTMzNDAzNSAxAA
    

    Now, just add the following line as a sub tag to the object tag

       <string key="IBUIColorCocoaTouchKeyPath">groupTableViewBackgroundColor</string>

    so that the complete section looks something like this:

       
          1
          MC45ODQ3MTEyMDQ3IDAuOTg4NTMzNDAzNSAxAA
          groupTableViewBackgroundColor
    

    That’s it – if you open the view in Interface Builder, you will now have a beauuutiful striped background.

     
    • Badisi 4:08 pm on May 31, 2010 Permalink | Reply

      In Interface Builder :
      – Click on the background property of your view,
      – Click on the third tab of the colors window,
      – In the palette drop down list choose : iphone SDK,
      – And finally : groupTableViewBackgroundColor

      Hope it helps

c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel