gpt4 book ai didi

php - 在 Laravel 中获取服务提供者内部的当前路线

转载 作者:行者123 更新时间:2023-12-02 06:56:15 24 4
gpt4 key购买 nike

首先,我必须说我对 Laravel 是全新的,所以如果你认为我的方式有误,请纠正我。

请注意,我的应用程序上安装了 Laravel 5.1。

我创建了一个应用程序,我喜欢将面包屑放在一个数组中,所以我决定创建一个服务提供者,服务提供者的代码如下:

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;

class BreadCrumbsServiceProvider extends ServiceProvider
{
// This will contain all the breadcrumb crumbs
protected $crumbs = [];


/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$this->createBreadcrumb();
}

/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->singleton(
'crumbs',
function() {
return $this->crumbs;
}
);
}

protected function createBreadcrumb( ) {

dd( Route::current() );

}
}

所以,当我运行我的网站时, dd仅返回 null .有人可以帮我解决这种情况吗?

最佳答案

在您的服务提供者类中 Route::Class 将不起作用。试试这些

当前请求 uri - $this->app->request->getRequestUri()
当前请求方法 - $this->app->request->getMethod()

关于php - 在 Laravel 中获取服务提供者内部的当前路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30915040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com