提问人:SybrenMeister 提问时间:11/16/2023 更新时间:11/16/2023 访问量:34
Laravel路由到我服务器上的js脚本并将其加载到页面
Laravel routing to a js script on my server and load that to page
问:
我想路由如下所示的请求:https://sub.domain.nl/tracking/MM-123/tracker.js
到我在这个目录中的javascript: 资源/js/Scrips/tracking/tracker.js
但是,看起来服务器/laravel 正在寻找这个脚本:https://sub.domain.nl/tracking/MM-123/tracker.js
在 /public 文件夹中。
但是,我希望它像从服务器中的普通视图/页面一样加载它。
我的网络.php路由器:
Route::group([], function () {
Route::get('/tracking/{script}/tracker.js', [TrackingScriptController::class, 'index'])->name('tracking');
});
我的跟踪脚本控制器.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\File;
class TrackingScriptController extends Controller
{
public function index($script)
{
// Construct the path to the script file
$filePath = resource_path("js/Scripts/Tracking/tracker.js");
// Check if the file exists
if (!File::exists($filePath)) {
// You can return a 404 or some other error if the file is not found
abort(404, 'Script not found');
}
// Read the contents of the file
$scriptContents = File::get($filePath);
// Return the contents with the correct Content-Type header
return Response::make($scriptContents, 200, ['Content-Type' => 'application/javascript']);
}
}
?>
但是浏览器在控制台中一直给出 404 页面未找到:
Failed to load resource: the server responded with a status of 404 () tracker.js
所以看起来我的 laravel 应用程序正在尝试在公共应用程序中查找此 .js 文件。当我像这样从 URL 中删除.js时:https://sub.domain.nl/tracking/MM-123/tracker
我的网络.php到这个:
Route::group([], function () {
Route::get('/tracking/{script}/tracker.', [TrackingScriptController::class, 'index'])->name('tracking');
});
它工作正常。
我正在使用 Laravel(Breeze 入门套件)+ Inertia + Vue。
有谁知道如何解决这个问题?不幸的是,在尝试了很多不同的东西之后,我不知道。
答: 暂无答案
评论