search
HomeBackend DevelopmentPHP TutorialMinor [PHP Framework] 3. Routing, controller, view, minor framework_PHP tutorial

Minor【PHP Framework】3. Routing, controller, view, minor framework

3.1 Routing

Pretty URLs are an absolute must for a serious web application. This way ugly URLs like index.php?article_id=57 are hidden and replaced by more popular URLs like /read/intro-to -symfony instead.

 3.1.1 Routing configuration

The configuration file is app/Config/routes.php

<?<span>php
</span><span>return</span><span> [
  </span>'/demo/{productName}'            =><span>  [
        </span>'name'                =>    'test1',
        'controller'        =>    'App\Modules\Demo\Controller\FooController',
        'action'            =>    'bar',
        'required'          =>  ['productName' => '\w+'],<span>
    ]</span>,<span>
];</span>

Take the above code as an example to describe in detail how to configure an elegant (laravel disease...) routing

 '/demo/{productName}' is the matching rule of routing. The required in the configuration specifies the regular conditions that productName must meet. During the actual operation of the framework, the routing rule required will be parsed into a regular expression/ demo/(w), when the url (eg: xxx.xxx.xxx/demo/testproduct) matches this regular rule, the action of the controller in the configuration will be executed. The specific execution is: FooController->bar($productName );

You can see that the content of the curly brackets (productName) in the routing rules is the parameter of the bar method. Therefore, when matching routes, be sure to note that the routing rules must be consistent with the number of parameters of the specific controller method, otherwise a ControllerException will be thrown.

 3.1.2 Default routing configuration

If we have to configure a route every time we define a Controller, it will lead to low development efficiency. In order to prevent this problem from happening, Minor provides a default routing mechanism. When we visit http://xxx.xxx.xxx/demo/foo/bar, the bar method of AppModulesDemoFooController will be executed, that is, the default route is:

 http://xxx.xxx.xxx/{module name}/{controller name}/{method name}

 3.1.3 Disadvantages

As you can see, Minor’s routing is not powerful. Request method restrictions, HTTPS restrictions, and filters are not supported.

3.2 Controller

 3.2.1 Create your own controller

The definition of Controller is very simple. Just inherit the MinorControllerController base class (of course you don’t have to inherit it, but the methods and properties in the base class cannot be used, which is easy to understand). First create the folder app/ Modules/Demo/Controller/, and then create the file FooController.php:

<?<span>php
namespace App\Modules\Demo\Controller;

</span><span>use</span><span> Minor\Controller\Controller;
</span><span>//</span><span> 定义一个控制器</span>
<span>class</span> FooController <span>extends</span><span> Controller
{
    </span><span>//</span><span> 定义一个方法   </span>
    <span>public</span> <span>function</span><span> bar()
    {
        </span><span>return</span> 'Hello World'<span>;
    }
}</span>

By accessing xxx.xxx.xxx/demo/foo/bar (default route, you can also configure your own route), you can see that Hello World is returned.

 3.2.2 Url generation

Calling Url’s gen method can convert the default path to a URL that conforms to routing rules

<span>$url</span> = Url:gen(<span>$path</span>);

If Url::gen('/demo/foo/bar?productName=test') is configured according to the routing in 3.1.1, /demo/test will be returned.

 

 3.2.3 Page jump redirect, redirect forward

Minor provides three jump methods: redirect, forward, and forwardUrl (these three are protected methods of MinorControllerController).

When jumping to another url, you can call it like this in the controller: $this->redirect($url);

When redirecting to another url, you can call it like this in the controller: $this->forwardUrl($url); (The implementation of this method is actually to parse out the controller and method of the url request through routing. Then call forward($controller, $action, $param))

When forwarding to another method, you can call it like this in the controller: $this->forward($controller, $action, $params); (The parameter $controller is the class name of the controller, including namespace)

Example:

<span>class</span> FooController <span>extends</span><span> Controller
{
    </span><span>public</span> <span>function</span><span> bar()
    {
        </span><span>$this</span>->redirect('www.baidu.com'<span>);
        </span><span>return</span> <span>$this</span>->forward('App\Modules\Demo\Controller\FooController', 'bar', 'test'<span>);
        </span><span>return</span> <span>$this</span>->forward('/demo/testpro'<span>);
    }
}    </span>

 3.2.4 Get request parameters

Call the get($paramName, $defaultParamValue = null) or post($paramName, $defaultParamValue = null) method of MinorRequest to get the request method. You can call it like this in the controller:

<span>class</span> FooController <span>extends</span><span> Controller
{
    </span><span>public</span> <span>function</span><span> bar()
    {
       $minorRequest = <span>$this->app->getMinorRequest()</span>; </span><br />    $paramValue = <span>$minorRequest</span>->get('paramKey', 'defaultValue'<span>);<br />    ...
    }
}   </span>

 3.2.5 Get request method

Call the getMethod() method of MinorRequest to get the requested method:

<span>class</span> FooController <span>extends</span><span> Controller
{
    </span><span>public</span> <span>function</span><span> bar()
    {
       </span><span>$minorRequest</span> = <span>$this</span>->app-><span>getMinorRequest(); 
    </span><span>$method</span> = <span>$minorRequest</span>-><span>getMethod();
    </span>...<span>
    }
}  </span>

3.3 View

Minor provides an extremely powerful template engine. The name of this template engine is: PHP. Yes! You read that right, PHP. Why doesn't Minor provide a template engine like smarty or Twig? Because there is no need, PHP itself is good enough. If Minor builds a template engine, it will undoubtedly make it more difficult to use Minor, so Minor directly uses PHP as the language for view files.

 3.3.1 Using views in controllers

To use a view in a controller, you only need to call View::render('module name:controller name:view file name', ['param1key' => 'param1value', 'param2key' => 'param2value' ...]); Example:

<span>class</span> FooController <span>extends</span><span> Controller
{
    </span><span>public</span> <span>function</span><span> bar()
    {
        </span><span>$param1</span> = 'Hello'<span>;
        </span><span>$param2</span> = 'World'<span>; 
        </span><span>return</span> View::render('Demo:Foo:bar.php', ['param1' => <span>$param1</span>, 'param2' => <span>$param2</span><span>]);
    }
}  </span>

  render函数的第二个参数(['param1key' => 'param1value', 'param2key' => 'param2value' ...])就是向视图文件中传递的变量,我们可以在视图文件中使用这些变量:

文件:app/Modules/Demo/Controller/Tpl/Foo/bar.php<br /><?php echo $param1key;?><br /><?=$param2key ?>

  3.3.2 视图内置函数

  Minor提供了两个视图文件中可以使用的函数:

<span>function</span> include_tpl(<span>$module</span>, <span>$controller</span>, <span>$tpl</span><span>)
{
    </span><span>require_once</span> (!<span>defined</span>('APP_DIR') ? APP_DIR : <span>realpath</span>(__DIR__ . '/../../app/') .DIRECTORY_SEPARATOR) . 'Modules' . '/' . <span>$module</span> . '/Tpl/' . <span>$controller</span> . '/' . <span>$tpl</span><span>;
}

</span><span>function</span> url(<span>$path</span><span>)
{
    </span><span>return</span> Url::gen(<span>$path</span><span>);
}</span>

  使用:

<span>html>   
    </span><span><?</span><span>php include_tpl('Public', 'Public', 'header.php');</span><span>?></span> 
    <span><</span><span>body</span><span>></span>
        <span><</span><span>h1</span><span>></span>Hello!<span></</span><span>h1</span><span>></span>
        <span><</span><span>a </span><span>href</span><span>="<?php url('/demo/foo/bar?productName=testpro');?>"</span><span>></span>
    <span></</span><span>body</span><span>></span>
<span></</span><span>html</span><span>></span>

  这两个函数定义在app/Resource/functions.php文件中,你可以在这个文件中自定义你需要的视图函数。

 

3.4 响应

  可以在控制器中通过调用App对象的getMinorResponse()方法来获取当前MinorResponse对象。 MinorResponse类提供了六个方法分别是:

<span>public function send(); // 用于将响应对象发送给客户端

public function setHeader($header);// 设置响应头

public function setContent($content); // 设置响应对象的内容

public function beforeContent($content); // 在当前已有的内容之前添加内容

public function appendContent($content); // 在当前已有内容之后追加内容

public function getContent(); // 获取对象中的响应内容</span>

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1135100.htmlTechArticleMinor【 PHP框架】3.路由、控制器、视图,minor框架 3.1 路由 漂亮的URL绝对是一个严肃的web应用程序必须做到的,这种方式使index.php?article_id...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
2023年最流行的11款PHP框架2023年最流行的11款PHP框架Jul 07, 2022 pm 03:30 PM

什么是PHP框架?为什么要使用PHP框架?本篇文章就来和大家聊聊PHP框架的优势,并总结分享11款2023年最流行的PHP框架,希望对大家有所帮助!

如何使用PHP框架Lumen开发一个高效的消息推送系统,提供及时的推送服务如何使用PHP框架Lumen开发一个高效的消息推送系统,提供及时的推送服务Jun 27, 2023 am 11:43 AM

随着移动互联网的快速发展和用户需求的变化,消息推送系统已成为现代应用程序不可或缺的一部分,它能够实现即时通知、提醒、推广、社交等功能,为用户和商业客户提供更好的体验和服务。为了满足这一需求,本文将介绍如何使用PHP框架Lumen开发一个高效的消息推送系统,提供及时的推送服务。一、Lumen简介Lumen是由Laravel框架开发团队开发的一个微框架,它是一个

2023年最流行的5个php开发框架视频教程推荐2023年最流行的5个php开发框架视频教程推荐May 08, 2017 pm 04:26 PM

如果想快速进行php web开发,选择一个好用的php开发框架至关重要,一个好的php开发框架可以让开发工作变得更加快捷、安全和有效。那2023年最流行的php开发框架有哪些呢?这些php开发框架排名如何?

什么是PHP框架?PHP框架与CMS的区别什么是PHP框架?PHP框架与CMS的区别Jun 13, 2022 pm 02:21 PM

在编程中,框架扩展了构建通用软件应用程序的支撑结构。在你开始编码之前,框架就会将程序的基本功能插入到你的应用程序中,从而简化了软件的开发过程。

安全加固PHP框架的实施措施安全加固PHP框架的实施措施Aug 07, 2023 pm 06:41 PM

标题:安全加固PHP框架的实施措施引言:随着互联网的快速发展,安全问题成为了一个不可忽视的挑战。而作为最常用的编程语言之一,PHP的安全性也备受关注。为了提高PHP框架的安全性,我们需要采取一系列的实施措施。本文将介绍一些基本的安全加固措施,并提供相应的代码示例。一、输入过滤和验证1.1XSS(跨站脚本攻击)过滤在PHP框架中,使用htmlspecialc

使用PHP框架CodeIgniter开发一个实时聊天应用,提供便捷的通讯服务使用PHP框架CodeIgniter开发一个实时聊天应用,提供便捷的通讯服务Jun 27, 2023 pm 02:49 PM

随着移动互联网的发展,即时通信变得越来越重要,越来越普及。对于很多企业而言,实时聊天更像是一种通信服务,提供便捷的沟通方式,可以快速有效地解决业务方面的问题。基于此,本文将介绍如何使用PHP框架CodeIgniter开发一个实时聊天应用。了解CodeIgniter框架CodeIgniter是一个轻量级的PHP框架,提供了一系列的简便的工具和库,帮助开发者快速

如何使用PHP框架Yii开发一个高可用的云备份系统如何使用PHP框架Yii开发一个高可用的云备份系统Jun 27, 2023 am 09:04 AM

随着云计算技术的不断发展,数据的备份已经成为了每个企业必须要做的事情。在这样的背景下,开发一款高可用的云备份系统尤为重要。而PHP框架Yii是一款功能强大的框架,可以帮助开发者快速构建高性能的Web应用程序。下面将介绍如何使用Yii框架开发一款高可用的云备份系统。设计数据库模型在Yii框架中,数据库模型是非常重要的一部分。因为数据备份系统需要用到很多的表和关

深入探讨swoole协程与PHP框架的结合开发深入探讨swoole协程与PHP框架的结合开发Aug 05, 2023 pm 01:54 PM

深入探讨swoole协程与PHP框架的结合开发国内的互联网发展迅速,更多的开发者开始寻找高性能的解决方案来满足日益增长的用户需求。在PHP领域,swoole协程是一个备受关注的技术,它可以大幅提升PHP的性能,并且非常适合与PHP框架结合使用。本文将深入探讨swoole协程与PHP框架的结合开发,并附带一些代码示例。一、什么是swoole协程swoole是一

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft