Using CakePhp 4.4.11
In my controller I have:
$languages = Hash::extract($user->languages, '{n}.name');
But when going to the page, I get: Class 'App\Controller\Hash' not found
Am I supposed to install something?
Using CakePhp 4.4.11
In my controller I have:
$languages = Hash::extract($user->languages, '{n}.name');
But when going to the page, I get: Class 'App\Controller\Hash' not found
Am I supposed to install something?
You are missing the use
statement at the top. Your IDE should help you with that usually.
something like use Cake\Utility\Hash;
So I put it in both AppController and UserController and am still getting the same error.
Request URL: /user/view/1
2025-03-16 14:42:54 error: [Error] Class 'App\Controller\Hash' not found in /Applications/MAMP/htdocs/toys/src/Controller/UserController.php on line 54
Stack Trace:
- /Applications/MAMP/htdocs/website/vendor/cakephp/cakephp/src/Controller/Controller.php:547
- /Applications/MAMP/htdocs/website/vendor/cakephp/cakephp/src/Controller/ControllerFactory.php:139
- /Applications/MAMP/htdocs/website/vendor/cakephp/cakephp/src/Controller/ControllerFactory.php:114
- /Applications/MAMP/htdocs/website/vendor/cakephp/cakephp/src/Http/BaseApplication.php:320
- /Applications/MAMP/htdocs/website/vendor/cakephp/cakephp/src/Http/Runner.php:86
- /Applications/MAMP/htdocs/website/vendor/cakephp/cakephp/src/Http/Middleware/CsrfProtectionMiddleware.php:174
- /Applications/MAMP/htdocs/website/vendor/cakephp/cakephp/src/Http/Runner.php:82
- /Applications/MAMP/htdocs/website/vendor/cakephp/cakephp/src/Http/Middleware/BodyParserMiddleware.php:157
- /Applications/MAMP/htdocs/website/vendor/cakephp/cakephp/src/Http/Runner.php:82
- /Applications/MAMP/htdocs/website/vendor/cakephp/cakephp/src/Routing/Middleware/RoutingMiddleware.php:186
- /Applications/MAMP/htdocs/website/vendor/cakephp/cakephp/src/Http/Runner.php:82
- /Applications/MAMP/htdocs/website/vendor/cakephp/cakephp/src/Routing/Middleware/AssetMiddleware.php:68
- /Applications/MAMP/htdocs/website/vendor/cakephp/cakephp/src/Http/Runner.php:82
- /Applications/MAMP/htdocs/website/vendor/cakephp/cakephp/src/Error/Middleware/ErrorHandlerMiddleware.php:131
- /Applications/MAMP/htdocs/website/vendor/cakephp/cakephp/src/Http/Runner.php:82
- /Applications/MAMP/htdocs/website/vendor/cakephp/debug_kit/src/Middleware/DebugKitMiddleware.php:60
- /Applications/MAMP/htdocs/website/vendor/cakephp/cakephp/src/Http/Runner.php:82
- /Applications/MAMP/htdocs/website/vendor/cakephp/cakephp/src/Http/Runner.php:67
- /Applications/MAMP/htdocs/website/vendor/cakephp/cakephp/src/Http/Server.php:90
- /Applications/MAMP/htdocs/website/webroot/index.php:40
Request URL: /user/view/1
Then you added it wrong. We can’t say exactly what you did that was wrong, as you haven’t shown us what you added or where. But this is not a “CakePHP issue”, it’s basic PHP. Cake docs assume a basic level of competence with PHP, maybe do a bit of reading on PHP namespaces to understand this better.
I added it to the top of the AppController.php file under:
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 0.2.9
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace App\Controller;
use Cake\Controller\Controller;
use Cake\Utility\Hash;
Under UserController.php, I have:
public function view($id = null)
{
$user = $this->Users->get($id, [
'contain' => ['Countries', 'Languages'],
]);
$languages = Hash::extract($user->languages, '{n}.name');
$this->set(compact('user'));
$this->set(compact('languages'));
}