Welcome Guest, Not a member yet? Register   Sign In
How to use URL to define response type
#1

[eluser]MikeVrind[/eluser]
At the moment I'm using a basic, self written framework for projects.
But I would like to make the transition to a more developed framework and I would like to try some things with CodeIgniter.

Now in my own framework a method to determine"output type". Through predefined keywords in the beginning of the URL I can send my data as JSON, XML or HTML (by default). Example: website.com/json/classname/functionName would return all the data as JSON. By removing JSON in the URL, the data is sent to the template.

I wondered if it's possible to build in this method in the CI framework. If so, what the best way/class to do this in?
#2

[eluser]rjsmith[/eluser]
With codeigniter, you'd probably do something like /class/method/type.
Your function would look like
Code:
public function method($type='html'){
    switch($type){
        case 'json':
        //code here
        break;
        ...
    }
}

Maybe in your routes you could define a route to move the type to the front

$routes['json/(:any)'] = '$1/json';
#3

[eluser]MikeVrind[/eluser]
I just started working this again after a little busy period.

I have the route added to the routes file. This worked very easy!
Only I used a slightly different approach for the route:
Code:
$route['json/(:any)'] = "$1/";

I Router I then made ​​a small adjustment to determine the request method.
This gives me the opportunity to use the router for getting the requestmethod.

Code:
$this->router->requestFormat

The default value of this var is HTML, but could be set to json or something else.

Judging by your example I should build a switch in every controller? This seems a bit awkward because it goes completely against the DRY (Dont repeat yourself) principle.

I'm just still looking into the framework for the best way to implement the final step, depending on the application method, outputing the data in the correct way (As HTML or JSON string).
Because I also want to add Twig as an template parser in the framework, I'm thinking of building a custom 'Output' classe that calls Twig in case of an HTML request and used json_encode() in case of an JSON request.
#4

[eluser]rjsmith[/eluser]
You can add it to a custom controller that extends CI_Controller, and use that controller as your base.
#5

[eluser]MikeVrind[/eluser]
Could u give a simple example of how this should be done?
#6

[eluser]rjsmith[/eluser]
Code:
//In application/core/My_Controller.php
class My_Controller extends CI_Controller{
    public function __construct(){
        parent::__construct();
    }
    public function html(){
        //code here
    }

    public function json(){
        //code here
    }

    public function xml(){
        //code here
    }
}

//In application/controllers/page.php
class Page extends My_Controller{
    //html, json, and xml methods still available and overloadable
}

My example code is different from what I suggested and may be closer to what you were initially wanting to do, but with a bit of tweaking it would still work with my first suggestion.




Theme © iAndrew 2016 - Forum software by © MyBB