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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:53:12+00:00 2026-06-03T17:53:12+00:00

I’m trying to understand MVC, and learning CI framework. I’ve some questions about MVC

  • 0

I’m trying to understand MVC, and learning CI framework. I’ve some questions about MVC and some basic questions about CI.

1)Views are visual part of application as i read from tutorials, my question is: e.g There is a button “Login” but if user already logged in button will be “Logout”. Where will that login check be? On controller or on view? i mean

   //this is view//

     <?php if($_SESSION('logged') == true):?>
     <a href="logout">Logout</a>
     <?php else: ?>
     <a href="login">login</a>        
     <?php endif; ?>

or

  //this is controller//

 if($_SESSION('logged') == true)
 $buttonVal = 'logout';
 else
 $buttonVal = 'login';

 //and we pass these value to view like
 $this->view->load('header',$someData);

 //this time view is like
 <a href="<?=$somedata['buttonVal']?>"><?=$somedata['buttonVal']?></a>

i just write theese codes as an example i know they wont work, they are imaginary codes, but i guess you got what i mean. Login check should be on controller or on view?

2)Should models contain only codes about data and return them to controller? For example there is a math, we get 2 value from database and multiply them and display them. Model will multiply or controller will do it?

here we load data with model and do math on controller:
//model

   $db->query(....);
   $vars=$db->fetchAll();

   return $vars;

   //controller
   $multi = $vars[0] * $vars[1];
   $this-load->view('bla.php',$mutli);

here we load data with model and do math on model too, controller just passes data from model to view:

   //model

   $db->query(....);
   $vars=$db->fetchAll();
   $multi = $vars[0] * $vars[1];
   return $multi;

   //controller
   $multi = $this->model->multiply();
   $this-load->view('bla.php',$mutli);

i mean with that, models should do only database works and pass data to controllers, controller do rest of work and send view to render? Or models do work, controllers get them and send them to view?

3)This is about codeigniter, i have a header which has to be in every page, but it has javascripts,css depending to page i’m using

        <?php foreach ($styles as $style): ?>
        <link id="stil" href="<?= base_url() ?>/css/<?= $style ?>.css" rel="stylesheet" type="text/css" />
        <?php endforeach; ?>

this will be on every page, so in every controller i have

$data['styles'] = array('css1','css2');
$this->load->view('header', $headers);

i’m thinking to make a main controller, write this in it, and all my others controllers will extend this, i see something MY_Controller on CI wiki, is this MY_Controller same with what i’m doing? Are there any other ways to do this?

Sorry for bad English and dummy questions. Thanks for answers.

  • 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-03T17:53:16+00:00Added an answer on June 3, 2026 at 5:53 pm

    This is absolutely view logic, the correct way to do it in my opinion:

     <?php if($logged_in):?>
     <a href="logout">Logout</a>
     <?php else: ?>
     <a href="login">login</a>        
     <?php endif; ?>
    

    The value of $logged_in would probably be retrieved from a call to a library method:

    <?php if ($this->auth->logged_in()): ?>
    

    Authentication is one of those things you’ll want access to globally, so you may be calling $this->auth->logged_in() in controller or views for different reasons (but probably not in models).

    In every controller i have

    $data['styles'] = array('css1','css2');
    $this->load->view('header', $headers);

    Yes you could extend the controller class with MY_Controller, but you’re better off keeping this in the view/presentation layer. I usually create a master template:

    <html>
    <head><!-- load assets --></head>
    <body id="my_page">
      <header />
      <?php $this->load->view($view); ?>
      <footer />
    </body>
    </html>
    

    And write a little wrapper class for loading templates:

    class Template {
      function load($view_file, $data) {
          $CI = &get_instance();
          $view = $CI->load->view($view_file, $data, TRUE);
          $CI->load->view('master', array('view' => $view));
      }
    }
    

    Usage in a controller:

    $this->template->load('my_view', $some_data);
    

    This saves you from loading header/footer repeatedly. In my opinion, presentation logic like which CSS file to load or what the page title should be belongs in the view whenever possible.

    As far as models go, you want them to be reusable – so make them do what you need and keep it strictly related to data manipulation (usually just your database). Let your controller decide what to do with the data.

    Not related to MVC, but in general you want to write as little code as possible. Redundancy is a sign that you could probably find a better solution. These are broad tips (as is your question) but hopefully it helps.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.