Livewire 是 Laravel 生态系统中最重要的项目之一,专门针对前端开发。 Livewire v3 最近发布了,让我们来探讨一下 Livewire 是什么,以及什么样的项目适合它的架构。
Livewire 的独特之处在于它允许开发“现代”Web 应用程序,而无需使用专用的 JavaScript 框架。
使用 Livewire,可以开发提供与 Vue 或 React 相同水平的反应性的 Blade 组件,而无需通过解耦的前端和后端来管理项目的复杂性。 您可以在 Laravel 和 Blade 模板的范围内继续开发您的应用程序。
Livewire 的工作原理
Livewire 是一个 Composer 包,您可以将其添加到 Laravel 项目中。然后必须使用适当的 Blade 指令在每个 HTML 页面(或该页面,如果您想要创建单页面应用程序)上激活它。 Livewire 组件由 PHP 类和 Blade 文件组成,其中包含特定前端组件如何工作以及必须呈现的逻辑。
当浏览器要求访问使用 Livewire 的页面时,会发生以下情况:
- 页面以组件的初始状态呈现,就像使用 Blade 创建的任何页面一样;
- 当组件 UI 触发交互时,将对适当的路由进行 AJAX 调用,指示 Livewire 组件和发生的交互,以及组件的状态;
- 数据在组件的 PHP 部分进行处理,该部分执行交互结果的新渲染并将其发送回浏览器;
- 页面的 DOM 根据从服务器收到的更改而更改。
它与 Vue 和 React 的做法非常相似,但在这种情况下,响应交互的反应性逻辑由后端管理,而不是在 javascript 端管理。
为了帮助您更好地理解逻辑,我将在下面向您展示此比较的示例。
如果您想了解更多有关建立开发人员驱动的公司所面临的挑战,您可以在 Linkedin 或 X 上关注我。
如何安装 Laravel Livewire
Livewire 安装绝对是最少的。在 Laravel 项目中安装 Composer 包,并将必要的 Blade 指令添加到所有页面(或项目中所有 Blade 模板所衍生的通用布局)。
composer require livewire/livewire
... @livewireStyles ... @livewireScripts
如何创建 Laravel Livewire 组件
安装 Composer 包后,可以使用新的 Artisan make 子命令来创建新的 Livewire 组件。每个组件都将由一个 PHP 类和一个 Blade 视图组成。
它类似于 Blade 基于类的组件。
php artisan make:livewire SpyInput COMPONENT CREATED ? CLASS: app/Http/Livewire/SpyInput.php VIEW: resources/views/livewire/spy-input.blade.php
此示例中的组件将“监视”HTML 输入字段中写入的内容,而无需编写 JavaScript 代码。
然后我们将公共属性插入到组件类中:
// app/Http/Livewire/SpyInput.php namespace App\Livewire; use Livewire\Component; class SpyInput extends Component { public string $message; public function render() { return view('livewire.spy-input'); } }
实现组件视图如下:
// resources/views/livewire/spy-input.blade.php <div> <label>Type here:</label> <input type="text" wire:model="message"> <span>You typed: <span>{{ $message }}</span></span> </div>
最后将 Livewire 组件放入刀片视图中:
@livewireStyles <spy-input></spy-input> @livewireScripts
在普通的 Blade 组件中,组件类的所有公共属性在 Blade 模板中都是可见的。所以在 {{ $message }} 中$message 属性的值将自动显示。然而,在普通的基于类的组件中,这只发生在第一个组件渲染时。如果您在输入字段中输入内容,span 标记中不会发生任何变化。
但是,在Livewire 组件中,我们在字段中使用了wire:model="message" 属性。该属性确保输入字段的值链接到 PHP 类中的 $message 属性。当您在输入字段中写入新值时,它将发送到服务器,服务器更新 $message 的值并执行新的渲染,将其发送回前端,然后更新 {{ 中的文本$message }}.
通过打开浏览器开发工具的“网络”选项卡,我们会注意到键盘上的每个按键都会按以下路线调用服务器:
/livewire/message/<component-name> </component-name>
对每个调用的响应都包含组件的新渲染 HTML,Livewire 会将其插入到页面中代替旧的 HTML。可以使用各种自定义导线属性。例如,您可以在单击按钮时执行组件类的公共方法。以下是此出价的示例:
<button wire:click="doSomething">Click Here</button>
class SpyInput extends Component { public function doSomething() { // Your code here… } }
其中 doSomething 是 Livewire 组件的 PHP 类的公共方法。
Integration with other Laravel features
The PHP class connected to the component behaves like any other PHP class in a Laravel project. The only difference is that it uses the mount method instead of the classic __construct class constructor to initialize the public properties of the class.
{{-- Initial assignment of the the $book property in the ShowBook class --}} <show-book :book="$book"> class ShowBook extends Component { public $title; public $excerpt; // "mount" instead of "__constuct" public function mount(Book $book = null) { $this->title = $book->title; $this->excerpt = $book->excerpt; } } </show-book>
You can also use the protected property $rules to configure the validation restrictions on the data sent from the frontend to the backend. You have to call the validate() method to validate the data:
class BookForm extends Component { public $title; public $excerpt; public $isbn; protected $rules = [ 'title' => ['required', 'max:200'], 'isbn' => ['required', 'unique:books', 'size:17'], 'excerpt' => 'max:500' ]; public function saveBook() { $validated = $this->validate($this->rules); Book::create($validated); return redirect()->to('/books); } }
Or you can use PHP Attributes to declare the desired validation rules for a class property:
class BookForm extends Component { #[Validate('required|max:200')] public $title; #[Validate('required|unique:books|size:17')] public $isbn; #[Validate('max:500')] public $excerpt; public function saveBook() { $this->validate(); Book::create([ 'title' => $this->title, 'isbn' => $this->isbn, 'excerpt' => $this->excerpt, ]); return redirect()->to('/books); } }
In general, each Livewire component behaves in the ways that a Laravel developer expects from a PHP class inside a Laravel project. Thus allowing the creation of reactive web interfaces without the need to separate the development projects between Laravel and Vue/React.
Monitor your Laravel application for free
Inspector is a Code Execution Monitoring tool specifically designed for software developers. You don't need to install anything at the server level, just install the Laravel package and you are ready to go.
If you are looking for HTTP monitoring, database query insights, and the ability to forward alerts and notifications into your preferred messaging environment, try Inspector for free. Register your account.
Or learn more on the website: https://inspector.dev
以上是Laravel Livewire:它是什么以及如何在您的 Web 应用程序中使用它的详细内容。更多信息请关注PHP中文网其他相关文章!

PHP类型提示提升代码质量和可读性。1)标量类型提示:自PHP7.0起,允许在函数参数中指定基本数据类型,如int、float等。2)返回类型提示:确保函数返回值类型的一致性。3)联合类型提示:自PHP8.0起,允许在函数参数或返回值中指定多个类型。4)可空类型提示:允许包含null值,处理可能返回空值的函数。

PHP中使用clone关键字创建对象副本,并通过\_\_clone魔法方法定制克隆行为。1.使用clone关键字进行浅拷贝,克隆对象的属性但不克隆对象属性内的对象。2.通过\_\_clone方法可以深拷贝嵌套对象,避免浅拷贝问题。3.注意避免克隆中的循环引用和性能问题,优化克隆操作以提高效率。

PHP适用于Web开发和内容管理系统,Python适合数据科学、机器学习和自动化脚本。1.PHP在构建快速、可扩展的网站和应用程序方面表现出色,常用于WordPress等CMS。2.Python在数据科学和机器学习领域表现卓越,拥有丰富的库如NumPy和TensorFlow。

HTTP缓存头的关键玩家包括Cache-Control、ETag和Last-Modified。1.Cache-Control用于控制缓存策略,示例:Cache-Control:max-age=3600,public。2.ETag通过唯一标识符验证资源变化,示例:ETag:"686897696a7c876b7e"。3.Last-Modified指示资源最后修改时间,示例:Last-Modified:Wed,21Oct201507:28:00GMT。

在PHP中,应使用password_hash和password_verify函数实现安全的密码哈希处理,不应使用MD5或SHA1。1)password_hash生成包含盐值的哈希,增强安全性。2)password_verify验证密码,通过比较哈希值确保安全。3)MD5和SHA1易受攻击且缺乏盐值,不适合现代密码安全。

PHP是一种服务器端脚本语言,用于动态网页开发和服务器端应用程序。1.PHP是一种解释型语言,无需编译,适合快速开发。2.PHP代码嵌入HTML中,易于网页开发。3.PHP处理服务器端逻辑,生成HTML输出,支持用户交互和数据处理。4.PHP可与数据库交互,处理表单提交,执行服务器端任务。

PHP在过去几十年中塑造了网络,并将继续在Web开发中扮演重要角色。1)PHP起源于1994年,因其易用性和与MySQL的无缝集成成为开发者首选。2)其核心功能包括生成动态内容和与数据库的集成,使得网站能够实时更新和个性化展示。3)PHP的广泛应用和生态系统推动了其长期影响,但也面临版本更新和安全性挑战。4)近年来的性能改进,如PHP7的发布,使其能与现代语言竞争。5)未来,PHP需应对容器化、微服务等新挑战,但其灵活性和活跃社区使其具备适应能力。

PHP的核心优势包括易于学习、强大的web开发支持、丰富的库和框架、高性能和可扩展性、跨平台兼容性以及成本效益高。1)易于学习和使用,适合初学者;2)与web服务器集成好,支持多种数据库;3)拥有如Laravel等强大框架;4)通过优化可实现高性能;5)支持多种操作系统;6)开源,降低开发成本。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

Atom编辑器mac版下载
最流行的的开源编辑器

Dreamweaver CS6
视觉化网页开发工具

Dreamweaver Mac版
视觉化网页开发工具