0
votes

I followed these instructions and created a view composer for my default layout.

My DefaultComposer.php is located under app/Http/ViewComposers, hence the namespace used below:

<?php namespace App\Http\ViewComposers;

use Illuminate\Contracts\View\View;

class DefaultComposer {

    public function compose(View $view) {

        $data['language'] = LanguageController::getDefaultLanguage();

        $view->with($data);
    }

}

?>

Now, when I load a page, I get the following error:

Class 'App\Http\ViewComposers\LanguageController' not found

This happens because the LanguageController.php is placed under app/Http/Controllers, which is a different namespace.

How can I use the LanguageController class in my DefaultComposer?


Update:

Using this declaration:

use App\Http\Controllers\LanguageController as LanguageController;

throws: Class 'App\Http\Controllers\LanguageController' not found. I'm confused.

1
Controllers aren't really meant to be used in this way. Laravel's config would be a better approach for this purpose. However, does \App\Http\Controllers\LanguageController::getDefaultLanguage() work? And does LanguageController definitely define the correct namespace of App\Http\Controllers?Jonathon
Yeah, I actually managed to get it to work. Posting answer in a few minutes. Thanks.lesssugar

1 Answers

1
votes

I figured this out. As the controllers in my application live in the global namespace, all I needed to do is add a backslash in front of the class name.

So instead of this:

$data['language'] = LanguageController::getDefaultLanguage();

I did this:

$data['language'] = \LanguageController::getDefaultLanguage();