Erste Schritte: MVC-Muster


Denken Sie an PHP6 Erste Schritte

  • ThinkPHP unterstützt das traditionelle MVC-Modell (Model-View-Controller) und das beliebte MVVM-Modell (Model-View-ViewModel) für die Anwendungsentwicklung

1 , MVC

  • MVC Das Softwaresystem ist in drei grundlegende Teile unterteilt: Modell (Model), Ansicht (View) und Controller (Controller)

  • ThinkPHP ist eine typische MVC-Architektur

Controller – Verantwortlich für die Weiterleitung von Anfragen und deren Bearbeitung.

Ansicht – Interface-Designer entwerfen grafische Interfaces.

Modell – Programmierer schreiben die Funktionen, die ein Programm haben sollte (Implementierung von Algorithmen usw.), Datenbankexperten führen die Datenverwaltung und das Datenbankdesign durch (können bestimmte Funktionen realisieren).

mvc.gif

2. Zugriff im Einzelanwendungsmodus

  • Projektzugriffspfad: www.xxx.com/index.php/ index/index

index.php Eintragsdatei

<<> Indexcontroller

Indexoperation

├ ├ ├ ├ ├ ├ ├ ├ ├ ├ 入 入 入 入 入 入 入 入🎜>│型 ─ Modellmodellverzeichnis

│ ├ ─ ... Weitere Arten von Bibliotheksverzeichnissen

│ │>

│ ├ ─ Common.php Öffentliche Funktionsdatei

│ └─event.php Ereignisdefinitionsdatei

├─config Konfigurationsverzeichnis

│ ├─app.php Anwendungskonfiguration

│ Anwendungskonfiguration Cookie-Konfiguration

│ ├─database.php Datenbankkonfiguration

│ ├─filesystem.php Dateifestplattenkonfiguration

│ ├─lang.php Mehrsprachige Konfiguration

│ ├─log.php Protokollkonfiguration

│ ├─middleware.php Middleware-Konfiguration

│ ├─route.php URL- und Routing-Konfiguration

│ ├─session.php Sitzungskonfiguration

│ ├─trace.php Trace-Konfiguration

│ └─view.php Konfiguration anzeigen

├─Ansicht         Verzeichnis anzeigen

├─Route                                                                               

│ ├─route.php Routendefinitionsdatei

│ └─ ...

├─öffentlich WEB-Verzeichnis (externes Zugriffsverzeichnis)

│ ├─index.php Eintragsdatei

│ ├─router.php Schnelltestdatei

│ └─.htaccess Umschreiben für Apache

├─extend Erweitertes Klassenbibliotheksverzeichnis

├─runtime Laufzeitverzeichnis der Anwendung (beschreibbar, anpassbar)

├─vendor Composer-Klassenbibliotheksverzeichnis

├─.example.env Beispieldatei für Umgebungsvariablen

├─composer .json Composer-Definitionsdatei

├─LICENSE.txt Autorisierungsbeschreibungsdatei

├─README.md README-Datei

├─think Befehlszeileneintragsdatei

3. Ansicht installieren

  • Die Ansichtsfunktion wird durch thinkView类 zusammen mit dem Ansichtstreiber (d. h. dem Template-Engine-Treiber) abgeschlossen )-Klasse, die neue Version. Nur die native PHP-Template-Engine ist integriert (wird hauptsächlich für die integrierte Ausnahmeseitenausgabe verwendet. Wenn Sie andere Template-Engines verwenden müssen, müssen Sie diese separat installieren). Die Template-Engine-Erweiterung

ThinkPHP6 verfügt über einen unabhängigen Satz von Vorlagen mit dem Namen: ThinkTemplate-Template-Engine

Um die Think-Template-Template-Engine zu verwenden, muss think-view installiert sein ;

Komponist erfordern topthink/think-view

  • Das Ansichtsverzeichnis kann im Stammverzeichnis oder im App-Anwendungsverzeichnis liegen

IV, Vorlagenrendering

  • Um View verwenden zu können, müssen Sie es zuerst importieren thinkfacadeViewFassadentyp

  • holen Methode zum Rendern der Seite

Parameter: der Pfad der statischen Seite, die standardmäßig entsprechende statische Seite

Controller-Code

<?php

Namespace-Appcontroller;

Verwendung thinkfacadeView;

class Index{

public function index(){

return View::fetch();

}

}

Ansichtscode

Hinweis: Die Indexklasse entspricht dem Verzeichnis unter der Ansicht

Hinweis : Die Indexmethode entspricht den statischen Dateien im Verzeichnis unter der Ansicht

5. Vorlagenvariablen

  • zuweisen Die Methodenzuweisung gehört zur globalen Variablenzuweisung

  • Vorlagenausgabe{$name}

Controller-Code

Namespace-Appcontroller ;

verwenden thinkfacadeView;

class Index{

public function index(){

// Vorlagenvariablenzuweisung

View::assign('name','Ouyang Ke');

View::assign('email','oyk@php.cn');

// Oder Batch-Zuordnung

Anzeigen :: zuweisen ([

'name' = & gt; 'Ouyang Ke',

'email' = & gt; 'oyk@php.cn'

]);

// Vorlagenausgabe

return View::fetch();

}

}

Code anzeigen

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>ThinkPHP6</title>

</head>

<body>

Name: {$name}

<br>

E-Mail: {$email}

</body>

</html>

6. Eintritt

  • Das öffentliche Verzeichnis ist das einzige über das Internet zugängliche Verzeichnis

  • Legen Sie die Ressourcendateien in public/static

  • <🎜 ab >
  • Eintragsdatei, der Standardwert ist index.php

12870f3d5e20e2e356baf864a59c6ee.png

Beispiel

  • Put htm lDie Seite wird im ThinkPHP-Framework platziert

  • Diese Seite ist eine statische Datei

<!DOCTYPE html>

<html>

<head>

<title>Listenseite</title>

<link rel= " Stylesheet" type="text/css" href="layui/css/layui.css">

<script type="text/javascript" src="layui/layui.js"></script>

<style type="text/css">

.header{width:100%;height: 50px;line-height: 50px;background: #2e6da4;color:#ffffff;}

.title{margin-left: 20px;font-size: 20px; }

.userinfo{float: right;margin-right: 10px;}

.userinfo a{color:#ffffff;}

.menu{width: 200px;background:#333744;position:absolute;}

.main{position: absolute;left:200px;right:0px;}


. layui-collapse{border: keiner;

        .layui-colla-content{border-top:none;padding:0px;}


        .content span{background: #009688;margin-left: 30px ;Polsterung: 10px;color:#ffffff;}

        .content div{border-bottom: solid 2px #009688;margin-top: 8px;}

        .content button{float: right;margin- oben: -5px;}

   </style>

</head>

<body>

    <div>

        <span><span style ="Schriftgröße: 20px;">XXX</span>--后台管理系统</span>

        <span>【欧阳克】<span><a href="javascript:;">退出</a></span></span>

    </div>

    <div id="menu" >

        <div Laien-Akkordeon>

            <div>

                <h2>商城管理</h2>

               <div class="layui-colla-contentlayui-show">

                    <ul class="layui-navlayui-nav-tree"lay-filter="test">

                       <li><a href="list.html">商品列表</a></li>

                        <li><a href="list.html">商品列表</a></li>

                        <li><a href="list.html">商品列表</a></li>

                        <li><a href="list.html">商品列表</a></li>

                    </ul>

               </div>

            </div>

                                                                                                      <div>

href="list.html">Produktliste</a></li>

                                                                            href="list.html">Produktliste</a></li>

                                                                            href="list.html">Produktliste</a></li>

                                                                                    href = "list.html" & gt; Produktliste & lt;/a & gt; & lt;/li & gt;

</div>

                                                                                            <h2>Einkaufszentrumsmanagement</h2>

                                                                                                                lay-filter = "test">                                                                           href="list.html">Produktliste</a></li>

                                                                                    href = "list.html" & gt; Produktliste & lt;/a & gt; & lt;/li & gt;

</div>

                                                         ;

        </div>

    </div>

    <div style="padding:10px;">

       <div>

            <span>商品列表</span>

            <div></div>

        </div>

                                                                                                                                                                            <th>Kategorie</th>

                                                                                                   <th>Status</th>

                                                                                                                                                                                                       <td>1</td>

                                                                                    <td>Öffnen</td>

                                                  </tr>

                                                                                      & Lt; <td>Damenbekleidung</td>

                                                           <td>Öffnen</td>

                                                                                                             <tr>

                                                                                            <td>                                                                                      <td>2019-12-12</td>

                                                                                                                                                               <td>                                                                                    <td>2019-12-12</td>

                                                                                                                                                           <td>                                                                                    

                                                                                                             <tr>

                                                                                                  <td>                                                                                    <td>2019-12-12</td>

                                                                                                                   </table>

</div>

</body>

</html>

<script>

layui.use(['element','layer ','laypage'], function(){

var element =layui.element;

varlaypage =layui.laypage;

$ =layui.jquery;

layer =layui.layer;

resetMenuHeight();

});

// Menücontainerhöhe zurücksetzen

function resetMenuHeight(){

var height = document.documentElement - 50;

$('#menu').height(height);

}

</script>

Controller-Code, diese Daten sind Testdaten, die später aus der Datenbank gelesen werden

Namespace AppController;

verwenden Sie die thinkfacadeView;

Klasse Index{

public function index(){

$title = 'Mall';

$login = 'Ouyang Ke';

$left = [

         [

                                                                                                                                                                 [

                                                                                                                                                                                                                                                                                                                                                                                                        'title' => 'Produktkategorie',

]

]

],

                                                                                                                                                             [

<'> 'ID' = & gt; 3,

'title' = & gt;

                                                                                                                                                                     3. [

                                    'id' =>                                                                                                                                                       'title' => 'Auftragsverwaltung',

                                                                      'title'                                                                                                                                                                 [

                                                                                                                                                                                                                                                                                                                                                                                                           'title' => 'Persönliches Center',

                                                                                'id' => 9,

]

                                                                                                                                                                          = & gt; 1,

'title' = & gt; 'Xixi World Herbst und Winter 2019 neues langärmeliges Oberteil mit Kapuze, kunstvoller Folie und verdicktem Pullover im BF-Stil',

'🎜 >'Katze' => 'Damenbekleidung',

              'Preis' => 189,

                'Rabatt' => 6,

                    'Status' => 1,

                                                                                                                                                           // 'add_time' => 2; 'Damenbekleidung',

              'Preis' => 699,

                'Rabatt' => 7,

                        'Status' => 1,

                                                                                                                                                                   // 'add_time' => 3; 'Preis' => 179,

'Rabatt' => 8,

'Status' =>

                                                                             'add_time' => '1576080000'

],

[

'id' => 1;

                                                                                                                                                                                          '1576080000'

],

];

View::assign([

'title' => $title,

                                                                                                                                                                                                         $right

]);

return View::fetch();

}

}

Ansichtscode, Controller-Daten werden in der Ansicht verwendet

<!DOCTYPE html>

<html>

<head>

<title>{$title}--Backend-Managementsystem</title>

<link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">

<script type="text/javascript" src="/static/layui/layui.js "></script>

<style type="text/css">

.header{width:100%;height: 50px;line-height: 50px;background: #2e6da4;color:#ffffff;}

.title{margin-left: 20px;Schriftgröße: 20px;}

.userinfo{float: right;margin-right: 10px;}

.userinfo a{color:#ffffff;}

        .menu{width: 200px;background:#333744;position:absolute;}

        .main{position: absolute;left:200px;right:0px;}


       .layui-collapse{border: none;}

        .layui-colla-item{border-top:none;}

        .layui-colla-title{background:#42485b;color: #ffffff;}

       .layui-colla-content{border-top:none;padding:0px;}


        .content span{background: #009688;margin-left: 30px;padding: 10px;color:#ffffff;}

        .content div{border-bottom: solid 2px #009688;margin-top: 8px;}

        .content button{float: right;margin-top: -5px;}

   </style>

</head>

<body>

    <div>

        <span><span style ="Schriftgröße: 20px;">{$title}</span>--后台管理系统</span>

        <span>【{$login}】<span><a href="javascript:;">退出</a></span></span>

    </div>

    <div id="menu" >

        <div Laien-Akkordeon>

            <div>

                <h2>{$left.0.title}</h2>

                <div class="layui-colla-contentlayui-show">

                    <ul class="layui-navlayui-nav-tree"lay-filter="test">

<li><a href="index.html">{$left.0.lists.0.title}</a></li>

                        <li>< A href="index.html">{$left.0.lists.1.title}</a></li>

                    </ul>

               </div>

            </div>

            <div>

                <h2>{$left[1]['title']}</h2>

                <div>

                    <ul class="layui-nav layui-nav-tree" lay-filter="test">

                        <li><a href="index.html">{$left.1.lists.0.title}</a></li>

                        <li><a href="index.html">{$left.1.lists.1.title}</a></li>

                        <li><a href="index.html">{$left.1.lists.2.title}</a></li>

                        <li><a href="index.html">{$left.1.lists.3.title}</a></li>

                    </ul>

                </div>

            </div>

            <div>

                <h2>{$left.2.title}</h2>

                <div>

                    <ul class="layui-nav layui-nav-tree" lay-filter="test">

                        <li><a href="index.html">{$left.2.lists.0.title}</a></li>

                        <li><a href="index.html">{$left.2.lists.1.title}</a></li>

                        <li><a href="index.html">{$left.2.lists.2.title}</a></li>

                    </ul>

                </div>

            </div>

        </div>

    </div>

    <div style="padding:10px;">

       <div>

            <span>商品列表</span>

            <div></div>

        </div>

                                                                                                                                                                            <th>Kategorie</th>

                                                                                                   <th>Rabatt</th>

                                                                                                      <th>Hinzufügen. Zeit</th>

                                                       <tbody>

                                                                                                                                                                                                                                                                                                                                              <td>{$right.1.id}</td>

                                                                                     <td>{$right.1.cat}</td>

                                                                                   

                    <td>{$right.1.status}</td>

                   <td>{$right.0.discount}</td>

                    <td>{$right.1.add_time}</td>

               </tr>

                <tr>

                    <td>{$right.2.id}</td>

                   <td>{$right.2.title}</td>

                    <td>{$right.2.cat}</td>

                 <td>{$right.2.price}</td>

                    <td>{$right.2.status}</td>

                   <td>{$right.0.discount}</td>

                    <td>{$right.2.add_time}</td>

               </tr>

                <tr>

                    <td>{$right.3.id}</td>

                   <td>{$right.3.title}</td>

                    <td>{$right.3.cat}</td>

                 <td>{$right.3.price}</td>

                    <td>{$right.3.status}</td>

                   <td>{$right.0.discount}</td>

                    <td>{$right.3.add_time}</td>

               </tr>

            </tbody>

        </table>

    </div>

</body>

</html>

<script>

   layui.use(['element','layer ','laypage'], function(){

        var element = layui.element;

        varlaypage =layui.laypage;

        $ =layui.jquery;

       layer =layui.layer;

       resetMenuHeight();

    });

    // 重新设置菜单容器高度

    function resetMenuHeight(){

        var height = document.documentElement.clientHeight - 50;

        $('#menu').height(height);

   }

</script>