3
votes

I'm learning laravel 5.2 and trying to make a cms project by following a video tutorial. I have created the files as follows-

app/View/Composers/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);
    }
}

app/Providers/AppServiceProvider.php

public function boot()
{
    $this->app['view']->composer('layouts.frontend', Composers\InjectPages::class);
}

resources/views/welcome.blade.php

@extends('layouts.frontend')

@section('title', 'Welcome')

@section('heading', 'This is a heading')

@section('content')

    <h1>Hello World</h1>

@endsection

public/themes/views/layouts/frontend.blade.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>@yield('title') &mdash; 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>

public/themes/views/partials/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

But always getting the following error.

ErrorException in Container.php line 734: Class App\Providers\Composers\InjectPages does not exist (View: /path/to/project/resources/views/welcome.blade.php)

I'm not sure, is these informations are sufficient to find out the error for you. If needed more information please let me know. Looking forward to your response. Thanks.

Updates-

After several tries as answer it seems to me the following file is also related to this error. That's why I'm adding this code also and sharing a dropbox link of my learning project.

app/View/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 link https://www.dropbox.com/sh/v8n1qvix20hywmo/AABVcs8DqvEhI89lw98mbxbya?dl=0

3
I've tried with App\View\Composers\InjectPages::class on AppServiceProvider.php. This time I'm getting Class App\Providers\App\View\Composers\InjectPages does not exist. One thing, My InjectPages.php is located in View folder. But is't searching in Providers folder? - biplob

3 Answers

0
votes

Try this:

composer('layouts.frontend', '\App\View\Composers\InjectPages::class');

Also, try to run composer dumpauto command.

0
votes

Check it out

composer('layouts.frontend', '\App\View\Composers\InjectPages');

or

composer('layouts.frontend', \App\View\Composers\InjectPages::class);
0
votes

Try by adding below code into composer.json

composer.json

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files":[
            "app/View/Composers/InjectPages.php"
    ]
},

Do : composer dump-autoload

in : app/Providers/AppServiceProvider.php

 public function boot()
  {
    view()->composer(
        'layouts.frontend', 'App\View\Composers\InjectPages'
    );
  }