1. #11
    Sencha User
    Join Date
    Jul 2009
    Location
    Jakarta, Indonesia
    Posts
    8
    Vote Rating
    0
    hariardi is on a distinguished road

      0  

    Default


    Quote Originally Posted by bclinton View Post
    I'm a little confused about what advantages there are to trying to integrate ExtJS into codeigniter. I'm not really sure what you guys are trying to do.

    Codeigniter is a PHP framework. I've been using it without feeling any need for extra integration. I overwrote CI_Controller with a MY_Controller class, but that was just for some utility methods like formatting my responses in JSON or JSONP depending on if a callback parameter is sent.

    Am I missing something here or are you guys making this more complex than it needs to be?

    edit: I guess I can see what you are trying to do with HMVC. I would just caution that it's not necessary so unless you are using HMVC for another reason, you can just have your codeigniter controllers spit out JSON rather than load views.
    OK, Im confuse
    I did this because I want to separate Extjs code and put it in single directory outside CI Application directory

  2. #12
    Sencha User Apeps's Avatar
    Join Date
    Jul 2009
    Posts
    3
    Vote Rating
    0
    Apeps is on a distinguished road

      0  

    Default


    Quote Originally Posted by bclinton View Post
    I'm a little confused about what advantages there are to trying to integrate ExtJS into codeigniter. I'm not really sure what you guys are trying to do.

    Codeigniter is a PHP framework. I've been using it without feeling any need for extra integration. I overwrote CI_Controller with a MY_Controller class, but that was just for some utility methods like formatting my responses in JSON or JSONP depending on if a callback parameter is sent.

    Am I missing something here or are you guys making this more complex than it needs to be?

    edit: I guess I can see what you are trying to do with HMVC. I would just caution that it's not necessary so unless you are using HMVC for another reason, you can just have your codeigniter controllers spit out JSON rather than load views.

    Hi bclinton can you share Your source code??

  3. #13
    Sencha User bclinton's Avatar
    Join Date
    Nov 2007
    Posts
    285
    Vote Rating
    9
    bclinton will become famous soon enough

      0  

    Default


    Quote Originally Posted by hariardi View Post
    OK, Im confuse
    I did this because I want to separate Extjs code and put it in single directory outside CI Application directory
    Let's say your directory is /myapp

    You unzip and copy codeigniter into there so you have:
    Code:
    myapp
    |-- application
    |-- system
    -- index.php
    Then, just make your ExtJS MVC directory in that top level directory per the MVC guide so you have
    Code:
    myapp
    |-- application
    |-- system
    |-- app
    |    |-- controller
    |    |-- model
    |    |-- store
    |    |-- view
    |-- ext-4.0
    |    |-- resources
    -- index.php
    -- app.js

  4. #14
    Sencha User bclinton's Avatar
    Join Date
    Nov 2007
    Posts
    285
    Vote Rating
    9
    bclinton will become famous soon enough

      0  

    Default


    Quote Originally Posted by Apeps View Post
    Hi bclinton can you share Your source code??
    There may be better ways to do this, but this is a simple way:

    Your main view:

    Code:
    <html>
    <head>
        <title>My app</title>
    
        <link rel="stylesheet" type="text/css" href="<?=base_url()?>ext-4.0/resources/css/ext-all.css">
    
        <script type="text/javascript" src="<?=base_url()?>ext-4.0/ext-debug.js"></script>
    
        <script type="text/javascript" src="<?=base_url()?>app.js"></script>
    
    </head>
    <body>
    </body>
    </html>
    Then I made a base controller class for my data responses so I could put common helper methods in it. When you extend CI_Controller and CI_Model you must use the MY prefix (or change it in the config file) and they go in the application/core directory.

    This isn't necessary, but you may find it useful for things like authentication. Here is an example with a method that wraps the response in a callback if necessary.
    Code:
    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    
    // application/core/MY_Controller.php
    
    class MY_Controller extends CI_Controller
    {
        function __construct()
        {
            parent::__construct();
    
            $this->load->helper(array('form', 'url'));
            $this->load->library('form_validation');
    
        }
        function sendResponse($results, $cb = null)
        {
            // if callback was sent use it, otherwise check GET
            $callback = $cb==null ? $this->input->get('callback', TRUE) : $cb;
    
            if ($callback)
            {
                // wrap json encoded response in callback
                // header('Content-Type: application/x-javascript; charset=utf-8');
                $result = $callback . '(' . json_encode($results) . ');';
            } else {
                // send json encoded response
                header('Content-Type: application/x-json; charset=utf-8');
                $result = json_encode($results);
            }
            echo $result;
        }
    }
    ?>
    I also extended model so I could add a method that formats my query results. Maybe this logic could or should go in the controller, but I decided to put it here:
    Code:
    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    
    // application/core/MY_Model.php
    
    class MY_Model extends CI_Model
    {
        function __construct()
        {
            parent::__construct();
    
        }
    
        /**
         * Check query validity and format result array
         *
         * @return  array
         */
        function formatQueryResult($query)
        {
            if ($query)
            {
                $query_result = $query->result();
                $total = $query->num_rows();
                $result = array (
                    'success' => true,
                    'total' => $total,
                    'rows' => $query_result
                );
            }
            else
            {
                $result = array (
                    'success' => false,
                    'msg' => 'Database Error: '.$this->db->_error_message(),
                    'num' => $this->db->_error_number()
                );
            }
            return $result;
        }
    }
    Then, an example controller might look like this:


    Code:
    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    
    class Clients extends MY_Controller
    {
        function __construct()
        {
            parent::__construct();
    
            $this->load->model('clientmodel');
        }
    
        function read()
        {
    
            $callback = $this->input->get('callback', TRUE);
    
            $results = $this->clientmodel->enumerate();
    
            $this->sendResponse($results,$callback);  // sendResponse method defined in MY_Controller
    
        }
    }
    and a model:

    Code:
    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    
    class Clientmodel extends MY_Model {
    
        function __construct()
        {
            parent::__construct();
        }
    
        /**
         * Get clients
         *
         * @return  array
         */
        function enumerate()
        {
            $this->db->select('Clients.id, Clients.name AS client_name');
            $this->db->select('Clients.address1, Clients.address2');
    
            $query = $this->db->get('Clients');
    
            return $this->formatQueryResult($query);  // formatQueryResult method defined in MY_Model
        }
    }
    I should probably mention that GET query strings are not enabled in Codeigniter by default. So if you are using JSONP you need to enable them in the codeigniter config.php to get the callback name.
    Code:
    $config['enable_query_strings'] = TRUE;
    If you're using regular JSON you can leave GET query strings turned off and just leave out all the GET/callback stuff above.

  5. #15
    Sencha User bclinton's Avatar
    Join Date
    Nov 2007
    Posts
    285
    Vote Rating
    9
    bclinton will become famous soon enough

      0  

    Default


    quote != edit

  6. #16
    Sencha User
    Join Date
    Jul 2009
    Location
    Jakarta, Indonesia
    Posts
    8
    Vote Rating
    0
    hariardi is on a distinguished road

      0  

    Thumbs up clear my view

    clear my view


    wow,
    I've never used this kind of method
    Thx alot bclinton

    I'll give it a try

  7. #17

    Default


    The problem I see with keeping CI and Ext separate is that both feature a MVC architecture. To properly do things, you need to implement your logic twice, one for CI in PHP, one for Ext in JS. So you end up with 2 controllers, at different levels of your application which doesn't really make sense. The way I see it is that the Ext files should be automatically generated by the CI controller (i.e a CI view). That would significantly reduce duplication of code. It does breaks Sencha recommendations though. Has anyone tried this approach before?

  8. #18
    Sencha User
    Join Date
    Jul 2009
    Location
    Jakarta, Indonesia
    Posts
    8
    Vote Rating
    0
    hariardi is on a distinguished road

      0  

    Default


    Quote Originally Posted by Thomas Triplet View Post
    The problem I see with keeping CI and Ext separate is that both feature a MVC architecture. To properly do things, you need to implement your logic twice, one for CI in PHP, one for Ext in JS. So you end up with 2 controllers, at different levels of your application which doesn't really make sense. The way I see it is that the Ext files should be automatically generated by the CI controller (i.e a CI view). That would significantly reduce duplication of code. It does breaks Sencha recommendations though. Has anyone tried this approach before?
    Yes, I've done that. I didn't really like it, because I have to mix it with PHP code in CI Views. With Extjs MVC, I don't need CI Views, because all I need to do is throw the json data and grab it with Ext.

    I know, I don't need CI for throwing json data, I can always use native PHP for that. But why not, I could use a little helper or a library from CI.

    that's my opinion & sorry for my bad english, correct & help me if I'm wrong.

  9. #19
    Sencha Premium Member vadimv's Avatar
    Join Date
    Sep 2010
    Location
    Chisinau, Moldova
    Posts
    642
    Vote Rating
    25
    vadimv will become famous soon enough vadimv will become famous soon enough

      0  

    Default


    Quote Originally Posted by Thomas Triplet View Post
    The problem I see with keeping CI and Ext separate is that both feature a MVC architecture. To properly do things, you need to implement your logic twice, one for CI in PHP, one for Ext in JS. So you end up with 2 controllers, at different levels of your application which doesn't really make sense. The way I see it is that the Ext files should be automatically generated by the CI controller (i.e a CI view). That would significantly reduce duplication of code. It does breaks Sencha recommendations though. Has anyone tried this approach before?
    IMO you will get an ugly app architecture, code and structure. Is not recommended to mix them, and from my experience there's no duplication of code at all , you have one CI view which loads the js files, one or more CI controllers, one or more CI models and just one extjs javascript file - each with different functionality.

    Also you could use the component's loader, for example if the app would need to suport user profiles , but again is not a mix, just jsons. It's a RIA app and extjs feels better in one js file

    A possible duplication of code could be when you use node.js as server, but depends on the app too.

  10. #20

    Default


    Agreed that it doesn't look pretty to mix JS and PHP, but keeping them separate does implies code duplication (duplication of classes, attributes, etc...)