我在 Apache 托管服务器上使用 Livewire 3.5 时遇到问题。为了测试这个库,我用正常的方式创建了一个简单的 Laravel 项目
composer create laravel/laravel progettotest
然后我将 Livewire 与
composer require livewire/livewire
生成的composer.json是:
{
"name": "laravel/laravel",
"type": "project",
"description": "The skeleton application for the Laravel framework.",
"keywords": ["laravel", "framework"],
"license": "MIT",
"require": {
"php": "^8.2",
"laravel/framework": "^11.9",
"laravel/tinker": "^2.9",
"livewire/livewire": "^3.5"
},......
现在,服务器有其他 Laravel 项目及其别名,因此,我也为这个基本项目配置了别名,如下所示:
Alias "/progettotest" "C:\PHP\progettotest\public"
<Directory "C:/PHP/progettotest/public">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
最后我在 Livewire 中创建了经典的“计数器”组件,这就是控制器:
namespace App\Livewire;
use Livewire\Component;
class Counter extends Component
{
public $count = 0;
public function increment()
{
$this->count++;
}
public function render()
{
return view('livewire.counter');
}
}
并创建了相应的刀片模板:
<div>
{{-- In work, do what you enjoy. --}}
<button wire:click="increment">+</button>
<h1>{{ $count }}</h1>
</div>
更改了主刀片模板(welcome.blade.php),如下所示:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
@livewireStyles
</head>
<body class="font-sans antialiased dark:bg-black dark:text-white/50">
<p>ciao</p>
@livewire('counter')
@livewireScripts
</body>
</html>
更改了 web.php 以更改 LiveWire 的脚本路由和更新路由:
<?php
use Illuminate\Support\Facades\Route;
use Livewire\Livewire;
Route::get('/', function () {
return view('welcome');
});
Livewire::setScriptRoute(function ($handle) {
return Route::get('/progettotest/livewire/livewire.js', $handle);
});
Livewire::setUpdateRoute(function ($handle) {
return Route::post('progettotest/vendor/livewire/update', $handle);
});
页面已加载,没有错误,Livewire.js 已加载,但是当我尝试单击组件的加号按钮时,浏览器上出现错误:
如果我使用 php artisan 服务器的内部服务器,我没有任何错误......任何人都可以帮助我吗?
谢谢。
我尝试将更新路由的路径更改为livewire.js。我尝试更改应用程序上主目录的 .htaccess,但问题始终相同。
只跑
php artisan livewire:发布