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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community