我正在学习laravel 5.2,并尝试按照视频教程制作cms项目。我创建了如下文件 -
应用程序/视图/作曲家/ InjectPages.php
namespace App\View\Composers;
use App\Page;
use Illuminate\View\View;
class InjectPages
{
protected $pages;
public function __construct(Page $pages)
{
$this->pages = $pages;
}
public function compose(View $view)
{
$pages = $this->pages->all()->toHierarchy();
$view->with('pages', $pages);
}
}
应用程序/提供者/ AppServiceProvider.php
public function boot()
{
$this->app['view']->composer('layouts.frontend', Composers\InjectPages::class);
}
资源/视图/ welcome.blade.php
@extends('layouts.frontend')
@section('title', 'Welcome')
@section('heading', 'This is a heading')
@section('content')
<h1>Hello World</h1>
@endsection
公共/主题/视图/布局/ frontend.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>@yield('title') — Tuts24.com</title>
<link rel="stylesheet" type="text/css" href="{{ theme('css/frontend.css') }}">
</head>
<body>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand">
<img src="{{ theme('images/logo.png') }}" alt="Tuts24.com">
</a>
</div>
<ul class="nav navbar-nav">
@include('partials.navigation')
</ul>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-12">
@yield('content')
</div>
</div>
</div>
</body>
</html>
公共/主题/视图/分音/ navigation.blade.php
@foreach($pages as $page)
<li class="{{ Request::is($page->uri_wildcard) ? 'active' : '' }} {{ count($page->children) ? ($page->isChild() ? 'dropdown-submenu' : 'dropdown') : '' }}">
<a href="{{ url($page->uri) }}">
{{ $page->title }}
@if(count($page->children))
<span class="caret {{ $page->child() ? 'right' : '' }}"></span>
@endif
</a>
@if(count($page->children))
<ul class="dropdown-menu">
@include('partials.navigation', ['pages' => $page->children])
</ul>
@endif
</li>
@endforeach
但总是会出现以下错误。
Container.php第734行中的ErrorException: 类App \ Providers \ Composers \ InjectPages不存在(查看:/path/to/project/resources/views/welcome.blade.php)
我不确定,这些信息是否足以为您找出错误。如果需要更多信息,请告诉我。 期待你的回复。感谢。
更新 -
经过多次尝试回答后,我觉得以下文件也与此错误有关。这就是为什么我也要添加这个代码并共享我学习项目的Dropbox链接。
应用程序/视图/ ThemeViewFinder.php
<?php
namespace App\View;
use Illuminate\View\FileViewFinder;
class ThemeViewFinder extends FileViewFinder
{
protected $activeTheme;
protected $basePath;
public function setBasePath($path)
{
$this->basePath = $path;
}
public function setActiveTheme($theme)
{
$this->activeTheme = $theme;
array_unshift($this->paths, $this->basePath.'/'.$theme.'/views');
}
}
Dropbox链接 https://www.dropbox.com/sh/v8n1qvix20hywmo/AABVcs8DqvEhI89lw98mbxbya?dl=0
答案 0 :(得分:0)
试试这个:
composer('layouts.frontend', '\App\View\Composers\InjectPages::class');
此外,尝试运行composer dumpauto
命令。
答案 1 :(得分:0)
检查出来
text in the first <p>: ' Hello World'
或
composer('layouts.frontend', '\App\View\Composers\InjectPages');
答案 2 :(得分:0)
尝试将以下代码添加到composer.json
中<强> composer.json 强>
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"files":[
"app/View/Composers/InjectPages.php"
]
},
执行: composer dump-autoload
in: app / Providers / AppServiceProvider.php
public function boot()
{
view()->composer(
'layouts.frontend', 'App\View\Composers\InjectPages'
);
}