mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
342 lines
13 KiB
HTML
342 lines
13 KiB
HTML
|
|
|
|
<!DOCTYPE html>
|
|
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
|
|
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
|
|
<head>
|
|
<meta charset="utf-8">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
<title>Models, Views, and Controllers — CodeIgniter4 4.0.0-alpha.4 documentation</title>
|
|
|
|
|
|
|
|
|
|
<link rel="shortcut icon" href="../_static/ci-icon.ico"/>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" />
|
|
|
|
|
|
|
|
|
|
|
|
<link rel="top" title="CodeIgniter4 4.0.0-alpha.4 documentation" href="../index.html"/>
|
|
<link rel="up" title="CodeIgniter4 Overview" href="index.html"/>
|
|
<link rel="next" title="Autoloading Files" href="autoloader.html"/>
|
|
<link rel="prev" title="Application Structure" href="structure.html"/>
|
|
|
|
|
|
<script src="../_static/js/modernizr.min.js"></script>
|
|
|
|
</head>
|
|
|
|
<body class="wy-body-for-nav" role="document">
|
|
|
|
|
|
<div class="wy-grid-for-nav">
|
|
|
|
|
|
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
|
|
<div class="wy-side-scroll">
|
|
<div class="wy-side-nav-search">
|
|
|
|
|
|
|
|
<a href="../index.html" class="icon icon-home"> CodeIgniter4
|
|
|
|
|
|
|
|
</a>
|
|
|
|
|
|
|
|
|
|
<div role="search">
|
|
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
|
|
<input type="text" name="q" placeholder="Search docs" />
|
|
<input type="hidden" name="check_keywords" value="yes" />
|
|
<input type="hidden" name="area" value="default" />
|
|
</form>
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Tutorial</a></li>
|
|
</ul>
|
|
<ul class="current">
|
|
<li class="toctree-l1 current"><a class="reference internal" href="index.html">CodeIgniter4 Overview</a><ul class="current">
|
|
<li class="toctree-l2"><a class="reference internal" href="structure.html">Application Structure</a></li>
|
|
<li class="toctree-l2 current"><a class="current reference internal" href="#">Models, Views, and Controllers</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="autoloader.html">Autoloading Files</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="services.html">Services</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="http.html">Working With HTTP Requests</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="security.html">Security Guidelines</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../general/index.html">General Topics</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../incoming/index.html">Controllers and Routing</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../outgoing/index.html">Building Responses</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../database/index.html">Working With Databases</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../models/index.html">Modeling Data</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../dbmgmt/index.html">Managing Databases</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Library Reference</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../testing/index.html">Testing</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../cli/index.html">Command Line Usage</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../extending/index.html">Extending CodeIgniter</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../license.html">The MIT License (MIT)</a></li>
|
|
<li class="toctree-l1"><a class="reference internal" href="../changelogs/index.html">Change Logs</a></li>
|
|
</ul>
|
|
|
|
|
|
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
|
|
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
|
|
|
|
|
|
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
|
|
|
|
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
|
|
<a href="../index.html">CodeIgniter4</a>
|
|
|
|
</nav>
|
|
|
|
|
|
|
|
<div class="wy-nav-content">
|
|
<div class="rst-content">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<div role="navigation" aria-label="breadcrumbs navigation">
|
|
|
|
<ul class="wy-breadcrumbs">
|
|
|
|
<li><a href="../index.html">Docs</a> »</li>
|
|
|
|
<li><a href="index.html">CodeIgniter4 Overview</a> »</li>
|
|
|
|
<li>Models, Views, and Controllers</li>
|
|
|
|
|
|
<li class="wy-breadcrumbs-aside">
|
|
|
|
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
|
|
<hr/>
|
|
</div>
|
|
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
|
|
<div itemprop="articleBody">
|
|
|
|
<div class="section" id="models-views-and-controllers">
|
|
<h1>Models, Views, and Controllers<a class="headerlink" href="#models-views-and-controllers" title="Permalink to this headline">¶</a></h1>
|
|
<p>Whenever you create an application, you have to find a way to organize the code to make it simple to locate
|
|
the proper files and make it simple to maintain. Like most of the web frameworks, CodeIgniter uses the Model,
|
|
View, Controller (MVC) pattern to organize the files. This keeps the data, the presentation, and flow through the
|
|
application as separate parts. It should be noted that there are many views on the exact roles of each element,
|
|
but this document describes our take on it. If you think of it differently, you’re free to modify how you use
|
|
each piece as you need.</p>
|
|
<p><strong>Models</strong> manage the data of the application, and help to enforce any special business rules the application might need.</p>
|
|
<p><strong>Views</strong> are simple files, with little to no logic, that display the information to the user.</p>
|
|
<p><strong>Controllers</strong> act as glue code, marshaling data back and forth between the view (or the user that’s seeing it) and
|
|
the data storage.</p>
|
|
<p>At their most basic, controllers and models are simply classes that have a specific job. They are not the only class
|
|
types that you can use, obviously, but the make up the core of how this framework is designed to be used. They even
|
|
have designated directories in the <strong>/app</strong> directory for their storage, though you’re free to store them
|
|
wherever you desire, as long as they are properly namespaced. We will discuss that in more detail below.</p>
|
|
<p>Let’s take a closer look at each of these three main components.</p>
|
|
<div class="section" id="the-components">
|
|
<h2>The Components<a class="headerlink" href="#the-components" title="Permalink to this headline">¶</a></h2>
|
|
<div class="section" id="views">
|
|
<h3>Views<a class="headerlink" href="#views" title="Permalink to this headline">¶</a></h3>
|
|
<p>Views are the simplest files and are typically HTML with very small amounts of PHP. The PHP should be very simple,
|
|
usually just displaying a variable’s contents, or looping over some items and displaying their information in a table.</p>
|
|
<p>Views get the data to display from the controllers, who pass it to the views as variables that can be displayed
|
|
with simple <code class="docutils literal"><span class="pre">echo</span></code> calls. You can also display other views within a view, making it pretty simple to display a
|
|
common header or footer on every page.</p>
|
|
<p>Views are generally stored in <strong>/app/Views</strong>, but can quickly become unwieldy if not organized in some fashion.
|
|
CodeIgniter does not enforce any type of organization, but a good rule of thumb would be to create a new directory in
|
|
the <strong>Views</strong> directory for each controller. Then, name views by the method name. This makes them very easy find later
|
|
on. For example, a user’s profile might be displayed in a controller named <code class="docutils literal"><span class="pre">User</span></code>, and a method named <code class="docutils literal"><span class="pre">profile</span></code>.
|
|
You might store the view file for this method in <strong>/app/Views/User/Profile.php</strong>.</p>
|
|
<p>That type of organization works great as a base habit to get into. At times you might need to organize it differently.
|
|
That’s not a problem. As long as CodeIgniter can find the file, it can display it.</p>
|
|
<p><a class="reference internal" href="../outgoing/views.html"><span class="doc">Find out more about views</span></a></p>
|
|
</div>
|
|
<div class="section" id="models">
|
|
<h3>Models<a class="headerlink" href="#models" title="Permalink to this headline">¶</a></h3>
|
|
<p>A model’s job is to maintain a single type of data for the application. This might be users, blog posts, transactions, etc.
|
|
In this case, the model’s job has two parts: enforce business rules on the data as it is pulled from, or put into, the
|
|
database; and handle the actual saving and retrieval of the data from the database.</p>
|
|
<p>For many developers, the confusion comes in when determining what business rules are enforced. It simply means that
|
|
any restrictions or requirements on the data is handled by the model. This might include normalizing raw data before
|
|
it’s saved to meet company standards, or formatting a column in a certain way before handing it to the controller.
|
|
By keeping these business requirements in the model, you won’t repeat code throughout several controllers and accidentally
|
|
miss updating an area.</p>
|
|
<p>Models are typically stored in <strong>/app/Models</strong>, though they can use a namespace to be grouped however you need.</p>
|
|
<p><a class="reference internal" href="../models/model.html"><span class="doc">Find out more about models</span></a></p>
|
|
</div>
|
|
<div class="section" id="controllers">
|
|
<h3>Controllers<a class="headerlink" href="#controllers" title="Permalink to this headline">¶</a></h3>
|
|
<p>Controllers have a couple of different roles to play. The most obvious one is that they receive input from the user and
|
|
then determine what to do with it. This often involves passing the data to a model to save it, or requesting data from
|
|
the model that is then passed on to the view to be displayed. This also includes loading up other utility classes,
|
|
if needed, to handle specialized tasks that is outside of the purview of the model.</p>
|
|
<p>The other responsibility of the controller is to handle everything that pertains to HTTP requests - redirects,
|
|
authentication, web safety, encoding, etc. In short, the controller is where you make sure that people are allowed to
|
|
be there, and they get the data they need in a format they can use.</p>
|
|
<p>Controllers are typically stored in <strong>/app/Controllers</strong>, though they can use a namespace to be grouped however
|
|
you need.</p>
|
|
<p><a class="reference internal" href="../incoming/controllers.html"><span class="doc">Find out more about controllers</span></a></p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
</div>
|
|
<div class="articleComments">
|
|
|
|
</div>
|
|
</div>
|
|
<footer>
|
|
|
|
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
|
|
|
|
<a href="autoloader.html" class="btn btn-neutral float-right" title="Autoloading Files" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
|
|
|
|
|
|
<a href="structure.html" class="btn btn-neutral" title="Application Structure" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
|
|
|
|
</div>
|
|
|
|
|
|
<hr/>
|
|
|
|
<div role="contentinfo">
|
|
<p>
|
|
© Copyright 2014-2019 British Columbia Institute of Technology.
|
|
Last updated on Dec 30, 2018.
|
|
|
|
</p>
|
|
</div>
|
|
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
|
|
|
|
</footer>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
</section>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<script type="text/javascript">
|
|
var DOCUMENTATION_OPTIONS = {
|
|
URL_ROOT:'../',
|
|
VERSION:'4.0.0-alpha.4',
|
|
COLLAPSE_INDEX:false,
|
|
FILE_SUFFIX:'.html',
|
|
HAS_SOURCE: false,
|
|
SOURCELINK_SUFFIX: ''
|
|
};
|
|
</script>
|
|
<script type="text/javascript" src="../_static/jquery.js"></script>
|
|
<script type="text/javascript" src="../_static/underscore.js"></script>
|
|
<script type="text/javascript" src="../_static/doctools.js"></script>
|
|
|
|
|
|
|
|
|
|
|
|
<script type="text/javascript" src="../_static/js/theme.js"></script>
|
|
|
|
|
|
|
|
|
|
<script type="text/javascript">
|
|
jQuery(function () {
|
|
SphinxRtdTheme.StickyNav.enable();
|
|
});
|
|
</script>
|
|
|
|
|
|
</body>
|
|
</html> |