Laravel使用管道處理名字, 實現統一處理
從程式碼的角度介紹管道的實際使用方式。有關管道的說明,網路上已有較多的篇幅介紹,自行查閱。
本篇部落格是使用管道處理名字, 實現統一處理的目的。背景:
本篇介紹是自己真實使用的過程的程式碼摘錄,親自測試,真實可用。只為拋磚引玉,不喜勿噴。
一、控制器
Route::get('/pipe', ['as'=>'pipe', 'uses'=>'PipeController@index']);
登入後複製
控製程式碼<?php
namespace App\Http\Controllers;
use App\Pipes\LeftWords;
use App\Pipes\RightWords;
use App\Pipes\BothSidesWords;
use Illuminate\Http\Request;
use Illuminate\Pipeline\Pipeline;
use App\User;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;
class PipeController extends Controller
{
/* 定义管道
*
* 第一步处理
* 第二部处理
* 第三部处理
* */
protected $pipes = [
LeftWords::class,
RightWords::class,
BothSidesWords::class,
];
// 首页
public function index(Request $request){
$name = $request->input('name');
// $name = Str::random(10);
return app(Pipeline::class)
->send($name)
->through($this->pipes)
->then(function ($content) {
return User::create([
'name' => $content,
'email'=>Str::random(10).'@gmail.com',
'password'=>Hash::make('password'),
]);
});
}
}
登入後複製
二、管道部分目錄結構如下:Route::get('/pipe', ['as'=>'pipe', 'uses'=>'PipeController@index']);
<?php namespace App\Http\Controllers; use App\Pipes\LeftWords; use App\Pipes\RightWords; use App\Pipes\BothSidesWords; use Illuminate\Http\Request; use Illuminate\Pipeline\Pipeline; use App\User; use Illuminate\Support\Str; use Illuminate\Support\Facades\Hash; class PipeController extends Controller { /* 定义管道 * * 第一步处理 * 第二部处理 * 第三部处理 * */ protected $pipes = [ LeftWords::class, RightWords::class, BothSidesWords::class, ]; // 首页 public function index(Request $request){ $name = $request->input('name'); // $name = Str::random(10); return app(Pipeline::class) ->send($name) ->through($this->pipes) ->then(function ($content) { return User::create([ 'name' => $content, 'email'=>Str::random(10).'@gmail.com', 'password'=>Hash::make('password'), ]); }); } }
├─app │ │ User.php │ ├─Http │ │ ...│ │ │ ├─Models │ │ ...│ │ │ ├─Pipes │ │ │ BothSidesWords.php │ │ │ LeftWords.php │ │ │ RightWords.php │ │ │ │ │ └─Contracts │ │ PipeContracts.php
interface
的程式碼- 路徑
- app/Pipes/Contracts/Pipe.php
下的程式碼如下:
<?php namespace App\Pipes\Contracts; use Closure; interface PipeContracts { public function handle($body, Closure $next); }
登入後複製三個管道的類別的程式碼
- 的程式碼
<?php namespace App\Pipes; use App\Pipes\Contracts\PipeContracts; use Closure; class LeftWords implements PipeContracts{ public function handle($body, Closure $next) { // TODO: Implement handle() method. $body = 'left-'.$body; return $next($body); } }
登入後複製LeftWords.php的程式碼
<?php namespace App\Pipes; use App\Pipes\Contracts\PipeContracts; use Closure; class RightWords implements PipeContracts{ public function handle($body, Closure $next) { // TODO: Implement handle() method. $body = $body.'-right'; return $next($body); } }
BothSidesWords.php的程式碼
<?php namespace App\Pipes; use App\Pipes\Contracts\PipeContracts; use Closure; class BothSidesWords implements PipeContracts{ public function handle($body, Closure $next) { // TODO: Implement handle() method. $body = '['.$body.']'; return $next($body); } }
handle ,你可以自訂方法名稱。像下面這樣定義
myHandleMethod
return app(Pipeline::class) ->send($name) ->through($this->pipes) ->via('myHandleMethod') ->then(function ($content) { return User::create([ 'name' => $content, 'email'=>Str::random(10).'@gmail.com', 'password'=>Hash::make('password'), ]); });
你這樣定義後,修改你的
interface,同時修改你的實作類別即可。
三、結果說明
http://localhost/pipe?name=lisa之後,能成功列印出所獲得的結果。 User
表內部,有資料儲存成功。{ "name": "[left-lisa-right]", "email": "3riSrDuBFv@gmail.com", "updated_at": "2020-09-05T05:57:14.000000Z", "created_at": "2020-09-05T05:57:14.000000Z", "id": 15 }
以上是Laravel使用管道處理名字, 實現統一處理的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

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

LaravelEloquent模型檢索:輕鬆獲取數據庫數據EloquentORM提供了簡潔易懂的方式來操作數據庫。本文將詳細介紹各種Eloquent模型檢索技巧,助您高效地從數據庫中獲取數據。 1.獲取所有記錄使用all()方法可以獲取數據庫表中的所有記錄:useApp\Models\Post;$posts=Post::all();這將返回一個集合(Collection)。您可以使用foreach循環或其他集合方法訪問數據:foreach($postsas$post){echo$post->

Laravel 是一款 PHP 框架,用於輕鬆構建 Web 應用程序。它提供一系列強大的功能,包括:安裝: 使用 Composer 全局安裝 Laravel CLI,並在項目目錄中創建應用程序。路由: 在 routes/web.php 中定義 URL 和處理函數之間的關係。視圖: 在 resources/views 中創建視圖以呈現應用程序的界面。數據庫集成: 提供與 MySQL 等數據庫的開箱即用集成,並使用遷移來創建和修改表。模型和控制器: 模型表示數據庫實體,控制器處理 HTTP 請求。

在使用CraftCMS開發網站時,常常會遇到資源文件緩存的問題,特別是當你頻繁更新CSS和JavaScript文件時,舊版本的文件可能仍然被瀏覽器緩存,導致用戶無法及時看到最新的更改。這個問題不僅影響用戶體驗,還會增加開發和調試的難度。最近,我在項目中遇到了類似的困擾,經過一番探索,我找到了wiejeben/craft-laravel-mix這個插件,它完美地解決了我的緩存問題。

Laravel 提供了一個全面的 Auth 框架,用於實現用戶登錄功能,包括:定義用戶模型(Eloquent 模型)創建登錄表單(Blade 模板引擎)編寫登錄控制器(繼承 Auth\LoginController)驗證登錄請求(Auth::attempt)登錄成功後重定向(redirect)考慮安全因素:哈希密碼、防 CSRF 保護、速率限制和安全標頭。此外,Auth 框架還提供重置密碼、註冊和驗證電子郵件等功能。詳情請參閱 Laravel 文檔:https://laravel.com/doc

利用地理空間技術高效處理700萬條記錄並創建交互式地圖本文探討如何使用Laravel和MySQL高效處理超過700萬條記錄,並將其轉換為可交互的地圖可視化。初始挑戰項目需求:利用MySQL數據庫中700萬條記錄,提取有價值的見解。許多人首先考慮編程語言,卻忽略了數據庫本身:它能否滿足需求?是否需要數據遷移或結構調整? MySQL能否承受如此大的數據負載?初步分析:需要確定關鍵過濾器和屬性。經過分析,發現僅少數屬性與解決方案相關。我們驗證了過濾器的可行性,並設置了一些限制來優化搜索。地圖搜索基於城

文章摘要:本文提供了詳細分步說明,指導讀者如何輕鬆安裝 Laravel 框架。 Laravel 是一個功能強大的 PHP 框架,它 упростил 和加快了 web 應用程序的開發過程。本教程涵蓋了從系統要求到配置數據庫和設置路由等各個方面的安裝過程。通過遵循這些步驟,讀者可以快速高效地為他們的 Laravel 項目打下堅實的基礎。

Laravel是如何在後端邏輯中發揮作用的?它通過路由系統、EloquentORM、認證與授權、事件與監聽器以及性能優化來簡化和增強後端開發。 1.路由系統允許定義URL結構和請求處理邏輯。 2.EloquentORM簡化數據庫交互。 3.認證與授權系統便於用戶管理。 4.事件與監聽器實現松耦合代碼結構。 5.性能優化通過緩存和隊列提高應用效率。

想要學習 Laravel 框架,但苦於沒有資源或經濟壓力?本文為你提供了免費學習 Laravel 的途徑,教你如何利用網絡平台、文檔和社區論壇等資源,從入門到掌握,為你的 PHP 開發之旅奠定堅實基礎。
