Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8807641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:25:04+00:00 2026-06-14T02:25:04+00:00

Is this a good practice to handle normal and ajax calls with one controller:

  • 0

Is this a good practice to handle normal and ajax calls with one controller:

<?php

class SomeController extends Controller {

    function index() {

         if(!$this->input->is_ajax_request()) {
             // load model
             // create form
             // pass data to view
             // ...
         } else {
            // validate input
            // load model
            // write data to database
            // return with some json string
         }

    }

}

What are the advantages and disadvantages?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:25:06+00:00Added an answer on June 14, 2026 at 2:25 am

    Short answer: it depends.

    Only real difference between XHR (what marketing people call “AJAX”) and ordinary browser request is that XHR expects a different form of response.

    In the MVC-inspired patterns for web the part that is responsible for generating response are the view instances. The view should recognize, which kind of response it has to produce, and act accordingly. Controllers role in this scenario would only be to change the state of current view.

    Alternatively, you can, at the bootstrap stage, detect the Accept HTTP header, and based on that initialize a different view instance.

    With “fully implement view” I mean an instance, which contains UI logic in the MVC triad and can decide which for to respond. This response can be HTML document, composed from multiple templates, a JSON/XML file or just a simple HTTP header.

    • Pros: proper separation of concerns, easier to maintain
    • Cons: have to implement full MVC

    .. but most of people do not use full MVC implementations.

    If you are one of people, who, instead of MVC-inspired patters, uses Rails-like variation about page controller pattern, then you will be force to create a separate controller for handling XHR.

    In this scenario the is no real view. It is replace by dumb template, while UI logic has been merged in the page controller. In this situation the only pragmatic option is to create a separate controller to deal with XHR.

    • Pros: simpler to implement in small projects
    • Cons: possible code repetition, harder to maintain
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it good practice to create a class like the one below that can
I'm looking at some code and I'm curious whether this is good practice. class
Is it a good practice to use one main AppDelegate.h to handle all the
Is this good practice? Storing objects accessed by a constant. There are plenty of
Is this legal and/or good practice? #define SOFTWARE_VERSION_NUMBER 7.0v1.1 Want struct to always contain
Is there any good practice for this? I wish I could solve the problem
Sorry if this is a bit random, but is it good practice to give
Is this good OO Design assuming you want every inheriting class to be a
Could someone please provide some good practice to handle exceptions in exception for example
I'm looking for advice on whether or not this is a good practice. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.