Support the ongoing development of Laravel.io →
posted 9 years ago
Security
Last updated 1 year ago.
0
Solution

It's a class namespaces related gotcha.

You should either add use Crypt; to the top of your file. Or use \Crypt instead of just Crypt. The backslash is important.

The problem is that controller class files set current namespace at the top of the file:

namespace App\Http\Controllers;

which is prepended to each unqualified class name you mention in this file. So when you call Crypt::encrypt('secret'); it actually becomes App\Http\Controllers\Crypt::encrypt('secret'), which is, obviously, not defined.

When you add use Crypt; at the top of your file you make an alias Crypt for Crypt class from global namespace which allows you to address it by unqualified name Crypt.

On the other hand, \Crypt is a fully qualified class name and it's resolved relative to global namespace, not to the one set in the current file by namespace expression. That's why it works, too.

PHP manual's chapter on aliases and importing (and a couple next chapters) actually explains it all very well.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.