简体   繁体   English

Laravel package 开发:没有为 [面包屑] 定义提示路径

[英]Laravel package development: No hint path defined for [breadcrumbs]

I have written a CMS package and am writing a bolt-on module to add events functionality.我已经编写了一个 CMS package 并且正在编写一个附加模块来添加事件功能。

Both of these packages use the Laravel Breadcrumbs library .这两个包都使用 Laravel面包屑库

This is part of the service provider for the events module:这是事件模块的服务提供者的一部分:

class EventsServiceProvider extends ServiceProvider
{
    // boot method

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

Breadcrumbs for the core CMS package all work fine until I try and load the additional breadcrumb routes for this events module package:核心 CMS package 的面包屑都可以正常工作,直到我尝试为此事件模块 package 加载额外的面包屑路由:

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

This then stops all breadcrumb routes from working and the following error is encountered:然后,这会阻止所有面包屑路由工作,并遇到以下错误:

No hint path defined for [breadcrumbs].没有为 [breadcrumbs] 定义提示路径。 (View: /Users/*****/Sites/****/CMS (Package)/src/views/admin/news/create.blade.php) (查看:/Users/*****/Sites/****/CMS(包)/src/views/admin/news/create.blade.php)

TL;DR TL;博士

You're trying to load the breadcrumps routes file from the same directory of your package using __DIR__您正在尝试使用__DIR__从 package 的同一目录加载面包屑路由文件

Reference the file from the base path从基本路径引用文件

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

The package itself reference it that way, see source package 本身就是这样引用它的,请参阅源代码

Explanation解释

__DIR__ is a predefined magic constant in PHP, see docs __DIR__是 PHP 中的预定义魔法常数,请参阅文档

__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.

It outputs the parent directory of the file executing it relatively它输出相对执行它的文件的父目录

Example例子
Given a directory structure like this给定这样的目录结构

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

file.php

<?php

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

Outputs输出

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

That's an incorrect path, routes directory is NOT inside folder这是一个不正确的路径, routes目录不在folder

However an index.php (a renamed copy of file.php) outputs但是index.php (file.php 的重命名副本)输出

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

Which is correct, because it's a relative path, index is in the parent directory of routes这是正确的,因为它是相对路径,索引在routes的父目录中

Solution?解决方案?

Use base_path() helper function since you're using Laravel使用base_path()助手 function 因为您使用的是 Laravel

The base_path function returns the fully qualified path to the project root. base_path function 将完全限定路径返回到项目根目录。 You may also use the base_path function to generate a fully qualified path to a given file relative to the project root directory:您还可以使用 base_path function 来生成相对于项目根目录的给定文件的完全限定路径:

$path = base_path();

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

It outputs a relative path to your index.php file in the public directory going back one step which is the root of your application它在公共目录中输出一个指向您的 index.php 文件的相对路径,这一步是您的应用程序的根目录

I hope this helps我希望这有帮助

I got this working by checking for the class and loading the routes in the boot method instead:我通过检查 class 并在boot方法中加载路由来完成这项工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM