国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 後端開發(fā) php教程 學(xué)習(xí)Slim Framework for PHP v3 (1)

學(xué)習(xí)Slim Framework for PHP v3 (1)

Jun 13, 2016 pm 12:28 PM
function return this

學(xué)習(xí)Slim Framework for PHP v3 (一)

  因為公司的項目用到是slim 框架,所以想把它學(xué)習(xí)一下。在公司用到是Slim2版本,現(xiàn)在官網(wǎng)已經(jīng)到達(dá) Slim3的版本了。官網(wǎng)地址:http://www.cnblogs.com/lmenglliren89php/。

  首先按照官網(wǎng)的教程,安裝Slim:

    1.curl -sS https://getcomposer.org/installer | sudo php --?--install-dir=/usr/local/bin --filename=composer

    2.composer require slim/slim "^3.0"

  這樣一個Slim就安裝好了。還有Apache的DirectDocumentroot設(shè)置:AllowOverride All。

  同時它自帶的Example的例子也是可以運行的,需要調(diào)整下文件夾的位置就好。

  

  如何才能理解一個框架呢?是很順利的使用嗎?還是從一步步去跟進(jìn)去它的流程?

?

  我的思路是這樣的,我把這個Example改成自己的項目,然后在不知道如何去的時候去深挖一下,不知道這樣的邏輯是否正確,暫且就這樣做吧。

  項目邏輯要求是這樣的,頁面提交數(shù)據(jù)--->>接收數(shù)據(jù)--->>存入數(shù)據(jù)庫--->>記入日志--->>返回寫入成功

  接收數(shù)據(jù)的route:

    

$app->get('/replace/', function ($request, $response, $args) {         Example\Module\Replace::instance()->setBody($request, $response);    });

  

  要說的是Slim就是基于route概念的,它將所有的request都轉(zhuǎn)給不同的route,然后每個route完成功能,最后設(shè)定response,請求完畢。

  get方法就是去設(shè)定一個route,以后的‘replace’就會匹配到那個閉包函數(shù)中。

  

  而這個route是放在哪里呢?在APP.php中有個contianer。這個container是個什么東西呢,來看代碼:

  

<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$container</span> =<span style="color: #000000;"> []){    </span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">is_array</span>(<span style="color: #800080;">$container</span><span style="color: #000000;">)) {        </span><span style="color: #800080;">$container</span> = <span style="color: #0000ff;">new</span> Container(<span style="color: #800080;">$container</span><span style="color: #000000;">);    }    </span><span style="color: #0000ff;">if</span> (!<span style="color: #800080;">$container</span><span style="color: #000000;"> instanceof ContainerInterface) {        </span><span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> InvalidArgumentException('Expected a ContainerInterface'<span style="color: #000000;">);    }    </span><span style="color: #800080;">$this</span>->container = <span style="color: #800080;">$container</span><span style="color: #000000;">;}</span>

  來看Container怎么做的:

public function __construct(array $values = []){    parent::__construct($values);    $userSettings = isset($values['settings']) ? $values['settings'] : [];    $this->registerDefaultServices($userSettings);}private function registerDefaultServices($userSettings){    $defaultSettings = $this->defaultSettings;    /**     * This service MUST return an array or an     * instance of \ArrayAccess.     *     * @return array|\ArrayAccess     */    $this['settings'] = function () use ($userSettings, $defaultSettings) {        return new Collection(array_merge($defaultSettings, $userSettings));    };    if (!isset($this['environment'])) {        /**         * This service MUST return a shared instance         * of \Slim\Interfaces\Http\EnvironmentInterface.         *         * @return EnvironmentInterface         */        $this['environment'] = function () {            return new Environment($_SERVER);        };    }    if (!isset($this['request'])) {        /**         * PSR-7 Request object         *         * @param Container $c         *         * @return ServerRequestInterface         */        $this['request'] = function ($c) {            return Request::createFromEnvironment($c->get('environment'));        };    }    if (!isset($this['response'])) {        /**         * PSR-7 Response object         *         * @param Container $c         *         * @return ResponseInterface         */        $this['response'] = function ($c) {            $headers = new Headers(['Content-Type' => 'text/html; charset=UTF-8']);            $response = new Response(200, $headers);            return $response->withProtocolVersion($c->get('settings')['httpVersion']);        };    }    if (!isset($this['router'])) {        /**         * This service MUST return a SHARED instance         * of \Slim\Interfaces\RouterInterface.         *         * @return RouterInterface         */        $this['router'] = function () {            return new Router;        };    }    if (!isset($this['foundHandler'])) {        /**         * This service MUST return a SHARED instance         * of \Slim\Interfaces\InvocationStrategyInterface.         *         * @return InvocationStrategyInterface         */        $this['foundHandler'] = function () {            return new RequestResponse;        };    }    if (!isset($this['errorHandler'])) {        /**         * This service MUST return a callable         * that accepts three arguments:         *         * 1. Instance of \Psr\Http\Message\ServerRequestInterface         * 2. Instance of \Psr\Http\Message\ResponseInterface         * 3. Instance of \Exception         *         * The callable MUST return an instance of         * \Psr\Http\Message\ResponseInterface.         *         * @param Container $c         *         * @return callable         */        $this['errorHandler'] = function ($c) {            return new Error($c->get('settings')['displayErrorDetails']);        };    }    if (!isset($this['notFoundHandler'])) {        /**         * This service MUST return a callable         * that accepts two arguments:         *         * 1. Instance of \Psr\Http\Message\ServerRequestInterface         * 2. Instance of \Psr\Http\Message\ResponseInterface         *         * The callable MUST return an instance of         * \Psr\Http\Message\ResponseInterface.         *         * @return callable         */        $this['notFoundHandler'] = function () {            return new NotFound;        };    }    if (!isset($this['notAllowedHandler'])) {        /**         * This service MUST return a callable         * that accepts three arguments:         *         * 1. Instance of \Psr\Http\Message\ServerRequestInterface         * 2. Instance of \Psr\Http\Message\ResponseInterface         * 3. Array of allowed HTTP methods         *         * The callable MUST return an instance of         * \Psr\Http\Message\ResponseInterface.         *         * @return callable         */        $this['notAllowedHandler'] = function () {            return new NotAllowed;        };    }    if (!isset($this['callableResolver'])) {        /**         * Instance of \Slim\Interfaces\CallableResolverInterface         *         * @param Container $c         *         * @return CallableResolverInterface         */        $this['callableResolver'] = function ($c) {            return new CallableResolver($c);        };    }}

  會看到這段代碼就是初始化container中的router key,以后的route都加到這個里面就好了。

<strong> $this['router'] = function () {   return new Router; };<br /> <br /> </strong> 只是能不加入自己的Key呢?<strong><br /></strong>

  第一次寫,就先這樣吧。

?

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Laravel 教程
1600
29
PHP教程
1502
276
C語言return的用法詳解 C語言return的用法詳解 Oct 07, 2023 am 10:58 AM

C語言return的用法有:1、對於傳回值類型為void的函數(shù),可以使用return語句來提前結(jié)束函數(shù)的執(zhí)行;2、對於傳回值型別不為void的函數(shù),return語句的作用是將函數(shù)的執(zhí)行結(jié)果傳回給呼叫者;3、提前結(jié)束函數(shù)的執(zhí)行,在函數(shù)內(nèi)部,我們可以使用return語句來提前結(jié)束函數(shù)的執(zhí)行,即使函數(shù)並沒有回傳值。

function是什麼意思 function是什麼意思 Aug 04, 2023 am 10:33 AM

function是函數(shù)的意思,是一段具有特定功能的可重複使用的程式碼區(qū)塊,是程式的基本組成單元之一,可以接受輸入?yún)?shù),執(zhí)行特定的操作,並傳回結(jié)果,其目的是封裝一段可重複使用的程式碼,提高程式碼的可重複使用性和可維護(hù)性。

Java中return和finally語句的執(zhí)行順序是怎樣的? Java中return和finally語句的執(zhí)行順序是怎樣的? Apr 25, 2023 pm 07:55 PM

原始碼:publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}#輸出上述程式碼的輸出可以簡單地得出結(jié)論:return在finally之前執(zhí)行,我們來看下字節(jié)碼層面上發(fā)生了什麼事情。下面截取case1方法的部分字節(jié)碼,並且對照源碼,將每個指令的含義註釋在

MySQL.proc表的作用與功能詳解 MySQL.proc表的作用與功能詳解 Mar 16, 2024 am 09:03 AM

MySQL.proc表的功能與功能詳解MySQL是一種流行的關(guān)係型資料庫管理系統(tǒng),開發(fā)者在使用MySQL時常常會涉及到預(yù)存程序(StoredProcedure)的建立與管理。而MySQL.proc表則是一個非常重要的系統(tǒng)表,它儲存了資料庫中所有的預(yù)存程序的相關(guān)信息,包括預(yù)存程序的名稱、定義、參數(shù)等。在本文中,我們將詳細(xì)解釋MySQL.proc表的作用與功能

'enumerate()'函數(shù)在Python中的用途是什麼? 'enumerate()'函數(shù)在Python中的用途是什麼? Sep 01, 2023 am 11:29 AM

在本文中,我們將了解enumerate()函數(shù)以及Python中「enumerate()」函數(shù)的用途。什麼是enumerate()函數(shù)? Python的enumerate()函數(shù)接受資料集合作為參數(shù)並傳回一個枚舉物件。枚舉物件以鍵值對的形式傳回。 key是每個item對應(yīng)的索引,value是items。語法enumerate(iterable,start)參數(shù)iterable-傳入的資料集合可以作為枚舉物件傳回,稱為iterablestart-顧名思義,枚舉物件的起始索引由start定義。如果我們忽

聊聊Vue2為什麼能透過this存取各種選項中屬性 聊聊Vue2為什麼能透過this存取各種選項中屬性 Dec 08, 2022 pm 08:22 PM

這篇文章帶大家解讀vue原始碼,來介紹一下Vue2中為什麼可以使用 this 存取各種選項中的屬性,希望對大家有幫助!

使用JavaScript中return關(guān)鍵字 使用JavaScript中return關(guān)鍵字 Feb 18, 2024 pm 12:45 PM

JavaScript中return的用法,需要具體程式碼範(fàn)例在JavaScript中,return語句用來指定從函數(shù)傳回的值。它不僅可以用於結(jié)束函數(shù)的執(zhí)行,還可以將一個值傳回給呼叫函數(shù)的地方。 return語句有以下幾個常見的用法:傳回一個值return語句可以用來傳回一個值給呼叫函數(shù)的地方。下面是一個簡單的範(fàn)例:functionadd(a,b){

如何在PHP中使用SOA函數(shù) 如何在PHP中使用SOA函數(shù) May 18, 2023 pm 01:10 PM

隨著網(wǎng)際網(wǎng)路的發(fā)展,SOA(服務(wù)導(dǎo)向的架構(gòu))成為了當(dāng)今企業(yè)級系統(tǒng)中的重要的技術(shù)架構(gòu)。 SOA架構(gòu)中的服務(wù)可以重複使用、重組和擴(kuò)展,同時也能夠簡化系統(tǒng)開發(fā)和維護(hù)的過程。 PHP作為一種被廣泛使用的Web程式語言,也提供了一些實作SOA的函數(shù)函式庫。接下來,我們將詳細(xì)介紹如何在PHP中使用SOA函數(shù)。一、SOA的基本概念SOA是分散式系統(tǒng)開發(fā)的思想與架構(gòu)

See all articles