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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:33:12+00:00 2026-05-28T01:33:12+00:00

I am planning on building a lightweight simple MVC in PHP for simple projects.

  • 0

I am planning on building a lightweight simple MVC in PHP for simple projects.

I am trying to grasp a simple route of showing a View template or returning a JSON response if it is an AJAX request.

Below is a snippet I just came up with, it is all hypothetical so none of the methods actually exist yet, it is just the way I would expect them to work.

Looking at the code below and reading the comments in it, does this look like how you would probably return an AJAX request instead of a view template in a basic MVC?

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


    function SomeAction($userId)
    {
        //GET ID from URI
        $this->request->get($userId['id']);

        // load a Model 'some' = Model Name
        $this->profile_model = $this->loadModel('some');

        // Get the Dataset from the Model
        $profileData = $this->profile_model->getProfile($userId);

        // Check if this is an AJAX request true/false
        if($this->request->isAjax()){
            //is AJAX is true then we return json instead of loading a View template
            return $this->response->json($profileData);
        }else{
            // load view file
            $this->view->load('userProfile', $profileData);
        }
    }
  • 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-28T01:33:13+00:00Added an answer on May 28, 2026 at 1:33 am

    I prefer this way in your method:

    public function executeSomeAction($request)
    {
        if ($request->isXmlHttpRequest())
        {
             $this->actAsJsonResponse(); // here you set headers and other things
             /**
              * $code - 0, if all's fine, other on error
              * $message - verbal message that describe the error, or success
              * $extra - any data to send.
              */
             return $this->prepareJsonResponse($code, $message, $extra);
        }
    }
    

    As a result in my framework I have something like:

    public function executeSave(\Lighty\Request\Request $request)
    {
        $this->actAsJsonResponse();
        $p      = \Lighty\Model\ParticipantSqlAdapter::getInstance()->findById($this->getUser()->getIdParticipant());
        $data   = $request->get('profile', \Lighty\Request\Request::POST, array());
        if (count($data))
        {
            $form = new \Form\Profile(array(
                'default'   => array_merge($p->getAccount()->getArray(), $p->getArray(), $data),
            ));
            if ($form->save($p))
            {
                if (isset($data['tags']))
                {
                    $tags = explode(',', $data['tags']);
                    array_trim($tags);
                    $tags = array_unique($tags);
                    $data['tags'] = implode(', ', $tags);
                    unset($tags);
                }
    
                $this->prepareMetaResponse(0, 'Your profile saved successfully', array('updated' => $data));
            }
            else
            {
                $this->prepareMetaResponse(1, 'Passed data is invalid', array('error' => $form->getErrors()));
            }
        }
        else
        {
            $this->prepareMetaResponse(2, 'Not enough data passed');
        }
    }
    

    Then you simplify processing request on client side:

    if (response.code == 0) {
        // do whatever you need to, on success
    } else {
        alert(response.code + ' ' + response.message);
    }
    

    Hope that helps.

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

Sidebar

Related Questions

i built an intranet on .NET MVC. I'm also building a separate planning tool
I'm planning on building a subsystem of one of my projects for laying out
I am currently in the planning stages of building a simple Django web app
I'm planning on building a web development template for a small team. I'm looking
I am building an application and I am planning on using OpenSSL for securing
I am building a dynamic form in a Cocoa application and am planning to
I am planning to familiarize( read teach ) myself with Silverlight by building an
Building a new ASP.net application, and planning to separate DB, 'service' tier and Web/UI
Im planning to create an Alert view with three buttons, my problem is I
I'm planning on building a mySQL table that requires a student's name which looks

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.