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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:23:53+00:00 2026-05-12T22:23:53+00:00

I have been reading a lot about how and why to use an MVC

  • 0

I have been reading a lot about how and why to use an MVC approach in an application. I have seen and understand examples of a Model, I have seen and understand examples of the View…. but I am STILL kind of fuzzy on the controller. I would really love to see a thorough enough example of a controller(s). (in PHP if possible, but any language will help)

Thank you.

PS: It would also be great if I could see an example of an index.php page, which decides which controller to use and how.

EDIT: I know what the job of the controller is, I just don’t really understand how to accomplish this in OOP.

  • 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-12T22:23:53+00:00Added an answer on May 12, 2026 at 10:23 pm

    Request example

    Put something like this in your index.php:

    <?php
    
    // Holds data like $baseUrl etc.
    include 'config.php';
    
    $requestUrl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    $requestString = substr($requestUrl, strlen($baseUrl));
    
    $urlParams = explode('/', $requestString);
    
    // TODO: Consider security (see comments)
    $controllerName = ucfirst(array_shift($urlParams)).'Controller';
    $actionName = strtolower(array_shift($urlParams)).'Action';
    
    // Here you should probably gather the rest as params
    
    // Call the action
    $controller = new $controllerName;
    $controller->$actionName();
    

    Really basic, but you get the idea… (I also didn’t take care of loading the controller class, but I guess that can be done either via autoloading or you know how to do it.)

    Simple controller example (controllers/login.php):

    <?php    
    
    class LoginController
    {
        function loginAction()
        {
            $username = $this->request->get('username');
            $password = $this->request->get('password');
    
            $this->loadModel('users');
            if ($this->users->validate($username, $password))
            {
                $userData = $this->users->fetch($username);
                AuthStorage::save($username, $userData);
                $this->redirect('secret_area');
            }
            else
            {
                $this->view->message = 'Invalid login';
                $this->view->render('error');
            }
        }
    
        function logoutAction()
        {
            if (AuthStorage::logged())
            {
                AuthStorage::remove();
                $this->redirect('index');
            }
            else
            {
                $this->view->message = 'You are not logged in.';
                $this->view->render('error');
            }
        }
    }
    

    As you see, the controller takes care of the “flow” of the application – the so-called application logic. It does not take care about data storage and presentation. It rather gathers all the necessary data (depending on the current request) and assigns it to the view…

    Note that this would not work with any framework I know, but I’m sure you know what the functions are supposed to do.

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

Sidebar

Related Questions

I use jQuery. I have been reading a lot about selector performance and optimizing
I have been reading a lot about MVC these days and I think I
I have been reading a lot about QR codes and how the code itself
I have been reading a lot about C++ casting and I am starting to
Lately I have been reading a lot of blog topics about big sites(facebook, twitter,
I have been reading a lot on MVC/MVP patterns.... I have a simple question....If
I've been reading a lot of stackoverflow questions about how to use the breadth-first
I have been googling a lot about this, but I can't find a specific
I have been reading a lot about JQuery's deferred object. And I can see
I've been reading a lot those days about reinterpret_cast<> and how on should use

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.