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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:00:18+00:00 2026-05-25T10:00:18+00:00

I am working on building a lightweight MVC, mainly for the learning process but

  • 0

I am working on building a lightweight MVC, mainly for the learning process but I would like it to be good enough to use eventually.

Below is a basic example/demo of how a basic controller might would look, let’s assume the URI has been processed and routed to this controller and these 2 methods.

1) I need to get data from database/cache/etc… inside my Model classes, I just need help on how I should load my models into my example controller below, you can see that I have added this below $profileData = $this->model->getProfile($userId) that is just made up and does not exist’s, how could I get something like that to work though? Or should I load the model into the class a different way?

2) A lot of pages will require a user to be logged into the site. SHould I process that part below in the controller to check if a user is logged in, example, before building the profile page, check if user is logged in, if not then build a login page instead and add these checks inside of each controller method/page?

/**
 * 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/
    function friends()
    {
        //GET data from a Model
        $friendsData = $this->model->getFriendlist();

        $this->view->load('userFriends', $friendsData);
    }
}

core

abstract class Core_Controller {
    protected $view;
    protected $model;

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


    }
}
  • 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-25T10:00:19+00:00Added an answer on May 25, 2026 at 10:00 am

    There are probably tons of ways to accomplish what you are trying.

    The “easiest” is probably to just override the constructor and instantiate the model directly.

    in User_Controller:

    public function __construct(DependencyContainer $dc) {
        parent::__construct($dc);
    
        $this->model = new User_Model();
    }
    

    I’m guessing that you are looking for something a little more automated though. If you want the Model to have the same name as the controller minus “_Controller”, just use get_class($this) in the constructor and use PHP’s string functions to parse out what you want. Once you have that in a variable, you can use that variable to instantiate the model:

    in Core_Controller:

    public function __construct(DependencyContainer $dc) {
        $this->view = new Core_View();
    
        // $model_class should be 'User_Model' now
        $model_class = str_replace('_Controller', '_Model', get_class($this));
    
        // now instantiate the model
        $this->model = new $model_class();
    }
    

    I haven’t actually worked with any framework that can only have one model associated with each controller (except may CakePHP? I can’t remember). With Symfony, the models and controllers are completely decoupled so you can use any model with any controller. You just instantiate the model as need. Symfony use the Doctrine ORM so for example, in a controller action, if you needed a model you would do something like this:

    $model = Doctrine::getTable('User');
    

    It might be worthwhile to consider a design more like that in order to promote a decoupled design and I promise that you will want more than one model in some controller at some point.

    2.) As far as authentication. Something that seems to be fairly common is to have some sort of setting (whether in a config file or a member variable) that says whether or not the current action needs the user to be authenticated. This is processed each time the action runs (Yii calls these kinds of things filters). If the user needs to be logged in, it stores the page that they are trying to access, and then redirects them to a log in page (you should only ever have to create one). Once they properly authenticate, it will redirect them back to where they were originally heading.

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

Sidebar

Related Questions

I'm working on building a pretty simple site mainly as an exercise in learning
I am working on building a GUI around a console application. I would like
I am working on building a new build box which I would like to
I'm currently working on building a java web app. I've been looking to use
I am working on building a HTML5 video player with a custom interface, but
I'm working on building an HTTPHandler that will serve up plain text for use
Background I'm working on building an ASP.Net MVC 3 application that utilizes: JQueryUI for
I am working on building a small php/mysql script that will act something like
I am working on building a little game program like this one for a
I'm working on building a development tool that is written in JavaScript. This will

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.