Category: Views

Q: Cakephp 3 read config value in helper class

I want to read domain's settings (usually hostname) from app.php config file in DomainHelper. How to read a value from cakephp's config file? I need to get config settings to build up urls for publisher's landing page.

<?php
namespace App\View\Helper;

use Cake\View\Helper\HtmlHelper;

use Cake\ORM\TableRegistry;

class DomainHelper extends HtmlHelper {

    public function getPublisherUrl($publisher_id) {

        $publisher = $TableRegistry->get('Publishers')->get($publisher_id);   

        //read server/domain url from configuration   
        $domain = $read_from_config;

        //publisher's url
        $url = $domain . '/p/' . $publisher['landing'];

        return $url;
    }
}
SOLVED helper configuration
1 Answers

Version: 3.0

Accepted by soar901225

With PHP's namespace simply add to Cake\Core\Configure to your class.

use Cake\Core\Configure;
<?php
namespace App\View\Helper;

use Cake\View\Helper\HtmlHelper;

use Cake\ORM\TableRegistry;
use Cake\Core\Configure;

class DomainHelper extends HtmlHelper {

    public function getPublisherUrl($publisher_id) {

        $publisher = $TableRegistry->get('Publishers')->get($publisher_id);   

        //read server/domain url from configuration   
        $domain = Configure::read('domain');

        //publisher's url
        $url = $domain . '/p/' . $publisher['landing'];

        return $url;
    }
}

Ref: Cakephp 3 Configuration

And remember to add your key/value to app.php configuration file.

<?php
return [
    /**
     * Debug Level:
     *
     * Production Mode:
     * false: No error messages, errors, or warnings shown.
     *
     * Development Mode:
     * true: Errors and warnings shown.
     */
    'debug' => filter_var(env('DEBUG', true), FILTER_VALIDATE_BOOLEAN),

    /**
     * Domain hostname
     */
    'domain' => 'http://www.example.com',

//..etc

Created: 30 Sep '16

Last Reply: 1 Oct '16

Replies: 1

Views: 731

Votes: 1

Welcome to Aero Coding!

Aero Coding is a CakePHP-focused Q&A community for professional and enthusiast cake bakers. It's built and run by you as part of the community.


Join Now Tour

Download Cakephp

Start baking your own CakePHP application!


Cakephp All Versions