CodeIgniter Forums

Full Version: Do I need to separate ajax methods from its main controller?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm so confused. I want to make my files organized.

Here's my scenario. I have to create an ajax requests in a single controller, lets call it "Dashboard". Now, I need to use ajax to get stats from my database to my dashboard every minute. So, to avoid confusion on where this ajax script belong, what I did was put all my ajax script to their corresponding controller.

PHP Code:
class Dashboard extends CI_Controller
{
    public function 
index()
    {
        
$this->load->view('my_dashboard');
    }

    
/**
     * Ajax
     */
    
public function ajax_stats_sales()
    {
        
// Code here
    
}

    public function 
ajax_stats_users()
    {
        
// Code here
    
}

    public function 
ajax_stats_prod()
    {
        
// Code here
    
}


Now, one idea pops out in my head. What if I put all AJAX scripts/pages to one controller so I keep it organized.
PHP Code:
class Ajax extends CI_Controller
{
    
// All ajax methods here. Not only on dashboard (ex: Ajax for User Accounts, Blogs, etc.)




Any ideas? What is the best practice for this?
It might sound like a cop-out, but I'd say it's completely up to you.

If you have limited or specific AJAX endpoints, I'd favour implementing them under Dashboard controller as separate method, but close to original controller.

On other hand, if you are going for full REST api route, you could be able to automate some of it, cutting down on amount of manually coding work required to set up all models.

As more back-end developer myself, it feels natural to me to go with the first solution, but there are some benefits on bigger projects, when you have react/angular front-end developers, it seems to make sense to give them api endpoints and let them fetch data they need.

It feels like overkill at first, but ability to hand over parts of project with ease is important too - but it's only likely scenario in bigger companies with bigger teams. It's about trade-off Smile
I'll go with the first solution. I came up with an idea.
To avoid myself from confusion, I will always append a prefix on all of my AJAX-related methods.

Code:
AJAX_login_user()

Then I will route it by declaring my router inside the routes.php

PHP Code:
$route['ajax/(:any)/(.+)'] = '$1/AJAX_$2'
Then on my JS files, I'll just use the following URL:
Code:
http://example.com/ajax/account/login-user

Take note that I enabled $route['translate_uri_dashes']

I don't know if this is a good idea, but, it worked!
I just need a piece of advice. Hahaha. Thank you, Pertti! +1 rep
(07-16-2018, 03:30 AM)kaitenz Wrote: [ -> ]I don't know if this is a good idea, but, it worked!

Being a developer is one of these weird careers where there are quite few ways to do the same task, so there is no one "true" way of doing things Smile

The more you work on different projects, the more you figure out what clients like to get and how can you make your own life easier as developer, but at first, specially when you are starting out, keep it simple and get it working functionally, rest will come with experience later.
(07-16-2018, 05:12 AM)Pertti Wrote: [ -> ]Being a developer is one of these weird careers where there are quite few ways to do the same task, so there is no one "true" way of doing things Smile

The more you work on different projects, the more you figure out what clients like to get and how can you make your own life easier as developer, but at first, specially when you are starting out, keep it simple and get it working functionally, rest will come with experience later.

Note that, new developers. Hahahaha. Thank you.
I like to keep my ajax functions in the same controller where they are being called from.

What I did is extend the Controller class (MY_Controller.php) and in there add a function called set_page_type($type), and in that function I can set if this is an ajax page or not. In that case, I have logic to handle Ajax request.

Then, in each Controller I make, I extend THAT class and then as the first step in a controller's specific function, set if it is an ajax handler or a regular page. Such as: $this->set_page_type('ajax); and then handle the ajax request.
I like the idea.

I'll add this one. I will also include the line below to avoid direct access to the page:

Code:
$this->input->is_ajax_request() OR show_404();