0

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.

2
  • 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? Commented Mar 26, 2015 at 15:28
  • Yeah, I actually managed to get it to work. Posting answer in a few minutes. Thanks. Commented Mar 26, 2015 at 15:31

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Or since they're in the global namespace you could have also done use LanguageController;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.