【问题标题】:Laravel package development: No hint path defined for [breadcrumbs]Laravel 包开发:没有为 [breadcrumbs] 定义提示路径
【发布时间】:2019-10-01 23:05:24
【问题描述】:

我已经编写了一个 CMS 包,并且正在编写一个附加模块来添加事件功能。

这两个包都使用 Laravel Breadcrumbs library

这是事件模块的服务提供者的一部分:

class EventsServiceProvider extends ServiceProvider
{
    // boot method

    // register method
    public function register()
    {
       $this->loadRoutesFrom(__DIR__ . '/routes/web.php');
    }
}

核心 CMS 包的面包屑都可以正常工作,直到我尝试为此事件模块包加载额外的面包屑路由:

public function register()
{
    $this->loadRoutesFrom(__DIR__ . '/routes/web.php');
    $this->loadRoutesFrom(__DIR__ . '/routes/breadcrumbs.php');
}

这会阻止所有面包屑路由工作,并遇到以下错误:

没有为 [面包屑] 定义提示路径。 (查看:/Users/*****/Sites/****/CMS(包)/src/views/admin/news/create.blade.php)

【问题讨论】:

  • 我不确定公司和客户名称是否与问题有关,但感谢您查看。

标签: php laravel laravel-5


【解决方案1】:

我通过检查类并在 boot 方法中加载路由来完成这项工作:

public function boot(Router $router)
{
  if (class_exists('Breadcrumbs')) {
       require __DIR__ . '/routes/breadcrumbs.php';
    }
}

【讨论】:

    【解决方案2】:

    TL;DR

    您正在尝试使用 __DIR__ 从包的同一目录加载面包屑路由文件

    从基本路径引用文件

    $this->loadRoutesFrom(base_path('routes/breadcrumbs.php'));
    

    包本身以这种方式引用它,请参阅source

    说明

    __DIR__ 是 PHP 中预定义的魔术常量,see docs

    __DIR__ The directory of the file. If used inside an include,
    the directory of the included file is returned.
    This is equivalent to dirname(__FILE__).
    This directory name does not have a trailing slash unless it is the root directory.
    

    输出相对执行文件的父目录

    示例
    给定这样的目录结构

    ~/test ✹ ★ ᐅ  tree 
    .
    ├── folder
    │   └── file.php
    ├── index.php
    └── routes
        └── breadcrumps.php
    

    file.php

    <?php
    
    echo __DIR__ . '/routes/breadcrump.php';
    

    输出

    /home/caddy/test/folder/routes/breadcrump.php
    

    这是一个不正确的路径,routes 目录在folderNOT

    但是index.php(file.php 的重命名副本)输出

    /home/caddy/test/routes/breadcrump.php
    

    正确,因为是相对路径,索引在routes的父目录中

    解决方案?

    使用 base_path() 辅助函数,因为您使用的是 Laravel

    base_path 函数返回项目根目录的完全限定路径。您还可以使用 base_path 函数生成给定文件相对于项目根目录的完全限定路径:

    $path = base_path();
    
    $path = base_path('vendor/bin');
    

    它会在公共目录中输出一个指向您的 index.php 文件的相对路径,该路径是您的应用程序的根目录

    希望对你有帮助

    【讨论】:

    • 谢谢 - 它应该从包中加载,因为它是现有面包屑路径之上的一组附加路由,它们以完全相同的方式从另一个包中加载
    • 我刚刚查看了我在其他包文件中的操作方式,并通过引导方法加载并现在可以使用 - 感谢您查看
    猜你喜欢
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 2021-10-05
    相关资源
    最近更新 更多