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 6552757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:29:57+00:00 2026-05-25T12:29:57+00:00

I am in the process of learning the MVC pattern and building my own

  • 0

I am in the process of learning the MVC pattern and building my own lightweight one in PHP

Below is a basic example of what I have right now.

I am a little confused on how I should handle AJAX requests/responses though.

In my example user controller below, If I went to www.domain.com/user/friends/page-14 in the browser, it would create a User object and call the friends method of that object

The friends method would then get the data needed for the content portion of my page.

My app would load a template file with a header/footer and insert the content from the object above into the middle of the page.

Now here is where I am confused, if a request is made using AJAX then it will call a page that will do the process over, including loading the template file. IF an AJAX call is made, I think it should somehow, just return the body/content portion for my page and not build the header/footer stuff.

So in my MVC where should I build/load this template file which will have the header/footer stuff? ANd where should I detect if an AJAX request is made so I can avoid loading the template?

I hope I am making sense, I really need help in figuring out how to do this in my MVC I am building. IUf you can help, please use some sample code

/**
* Extend this class with your Controllers
* Reference to the model wrapper / loader functions via $this->model
* Reference to the view functions via $this->view
*/
abstract class Core_Controller {
    protected $view;
    protected $model;

    function __construct(DependencyContainer $dependencyContainer){
        $this->view = new Core_View();
        //$this->view = $dependencyContainer->get(view);


    }

    public function load($model){
        //load model
        //this part under construction and un-tested
        $this->$model = new $model;

    }

}

user controller

/**
 * Example Controller
 */
class User_Controller extends Core_Controller {

    // domain.com/user/id-53463463
    function profile($userId)
    {
        //GET data from a Model
        $profileData = $this->model->getProfile($userId);

        $this->view->load('userProfile', $profileData);
    }

    // domain.com/user/friends/page-14
    function friends()
    {
        //GET data from a Model
        $friendsData = $this->model->getFriends();

        $this->view->load('userFriends', $friendsData);
    }
}
  • 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-05-25T12:29:58+00:00Added an answer on May 25, 2026 at 12:29 pm

    For me, I developed a separate object that handles all template display methods. This is good because you can then ensure that all the resources you need to display your UI is contained in one object. It looks like you’ve isolated this in Core_View.

    Then, when an AJAX call is made, simply detect that it is an AJAX call. This can be done by either making the AJAX call through an AJAX object, which then references other objects, or you can take an easy approach and simply set an extra POST or GET field which indicates an AJAX call.

    Once you’ve detected if it’s an AJAX call, define a constant in your MVC such as AJAX_REQUEST. Then, in your template/UI object, you can specify that if it’s an AJAX call, only output your response text. If it isn’t, proceed with including your template files.

    For me, I send it through an AJAX object. That way I don’t have to worry about making a single output work for both cases. When it’s ready to send a response, I just do something to the manner of print( json_encode( ...[Response]... ) ).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on building a lightweight MVC, mainly for the learning process but
I have been learning .MVC recently and am having trouble understanding how to process
I am in the process of learning MySQL and querying, and right now working
I am in the process of learning MVC 3 using the basic project template
I am in the process of learning MVC3 and have implemented my own MembershipProvider
I am in the process of learning the MVC pattern in practise, meaning that
On the process of learning Assembly i got one question If i do the
I'm in the process of learning Databases and SQL. From what I have read
I am in the process of learning the Yii Framework. I have been following
I'm currently in the process of learning ASP MVC and am running into a

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.