3.4.3 설정 클래스

CakePHP에는 별로 설정할 게 없지만 응용 프로그램을 위한 설정 처리가 필요할 수도 있습니다. 이전 같으면 몇몇 파일에다가 변수나 상수를 정의하는 식으로 설정값을 지정했을 겁니다. 그런데 그렇게 하자면 그 값들을 사용해야 할 때마다 그 설정 파일을 인클루드 해야 합니다.

CakePHP의 새로운 Configure 클래스를 이용해서 응용 내지는 실행 상태와 관련된 값들을 저장하거나 가져올 수 있습니다. 하지만 조심할 것이 하나 있습니다. 이 클래스에는 무엇이든 저장할 수 있고 코드 내의 어느 곳에서도 이를 사용할 수 있습니다. CakePHP의 설계 의도인 MVC 패턴을 깨는 유혹이 될 수 있는 것이죠. Configure 클래스의 주된 목표는 여러 오브젝트가 공유할 수 있는 변수들을 중앙집중식으로 모아두는 것입니다. "설정보다는 관례"에 따라 작업하는 걸 잊지 마십시오. 그래야 잘 만들어 놓은 MVC 구조를 깨버리지 않을 수 있습니다.

이 클래스는 싱글턴(singleton) 방식으로 동작하므로 응용 프로그램의 어디에서든 정적 방식으로 메소드를 호출할 수 있습니다.

<?php Configure::read('debug'); ?>
  1. <?php Configure::read('debug'); ?>

3.4.3.1 Configure의 메소드

3.4.3.1.1 write

write(string $key, mixed $value)
  1. write(string $key, mixed $value)

write()를 이용하면 응용 프로그램 설정에 데이터를 저장할 수 있습니다.

Configure::write('Company.name','Pizza, Inc.');
Configure::write('Company.slogan','Pizza for your body and soul');
  1. Configure::write('Company.name','Pizza, Inc.');
  2. Configure::write('Company.slogan','Pizza for your body and soul');

$key 인자에서 마침표를 사용한 걸 볼 수 있을 겁니다. 이런 표기 방식을 사용해서 설정을 논리적 그룹들로 조직화 할 수 있습니다.

위 예를 다음과 같이 한 번의 호출로 작성할 수도 있습니다.

Configure::write(
    'Company',array('name'=>'Pizza, Inc.','slogan'=>'Pizza for your body and soul')
);
  1. Configure::write(
  2. 'Company',array('name'=>'Pizza, Inc.','slogan'=>'Pizza for your body and soul')
  3. );

Configure::write(‘debug’, $int)라고 사용해서 디버그 모드와 프로덕션 모드를 간편하게 전환할 수도 있을 겁니다. 이는 특히 디버깅 정보가 파싱 문제를 일으킬 수 있는 AMF나 SOAP 처리에서 유용합니다.

3.4.3.1.2 read

read(string $key = 'debug')
  1. read(string $key = 'debug')

응용 프로그램의 설정 데이터를 읽어오는 데 쓰입니다. 키를 생략하면 CakePHP에서 상당히 중요한 debug 값을 읽어옵니다. 키를 주면 데이터를 얻을 수 있습니다. 앞서의 write() 예제를 이용하자면 다음과 같은 식으로 그 데이터를 다시 읽어올 수 있습니다.

Configure::read('Company.name');    //결과값: 'Pizza, Inc.'
Configure::read('Company.slogan');  //결과값: 'Pizza for your body and soul'
 
Configure::read('Company');
 
//결과값: 
array('name' => 'Pizza, Inc.', 'slogan' => 'Pizza for your body and soul');
  1. Configure::read('Company.name'); //결과값: 'Pizza, Inc.'
  2. Configure::read('Company.slogan'); //결과값: 'Pizza for your body and soul'
  3. Configure::read('Company');
  4. //결과값:
  5. array('name' => 'Pizza, Inc.', 'slogan' => 'Pizza for your body and soul');

3.4.3.1.3 delete

delete(string $key)
  1. delete(string $key)

응용 프로그램의 설정에서 정보를 삭제하는 데 쓰입니다.

Configure::delete('Company.name');
  1. Configure::delete('Company.name');

3.4.3.1.4 load

load(string $path)
  1. load(string $path)

이 메소드를 이용하면 특정 파일에서 설정 정보를 읽어올 수 있습니다.

// /app/config/messages.php:
<?php
$config['Company']['name'] = 'Pizza, Inc.';
$config['Company']['slogan'] = 'Pizza for your body and soul';
$config['Company']['phone'] = '555-55-55';
?>
 
<?php
Configure::load('messages');
Configure::read('Company.name');
?>
  1. // /app/config/messages.php:
  2. <?php
  3. $config['Company']['name'] = 'Pizza, Inc.';
  4. $config['Company']['slogan'] = 'Pizza for your body and soul';
  5. $config['Company']['phone'] = '555-55-55';
  6. ?>
  7. <?php
  8. Configure::load('messages');
  9. Configure::read('Company.name');
  10. ?>

모든 설정 키-값 짝은 파일 내의 $config 배열로 표현해야 합니다. load() 함수는 파일 내의 다른 변수들은 무시하게 됩니다.

3.4.3.1.5 version

version()
  1. version()

현재 응용 프로그램에서 사용중인 CakePHP의 버전을 반환합니다.

3.4.3.2 CakePHP Core Configuration Variables

The Configure class is used to manage a set of core CakePHP configuration variables. These variables can be found in app/config/core.php. Below is a description of each variable and how it affects your CakePHP application.

Configure Variable Description
debug Changes CakePHP debugging output.

0 = Production mode. No output.
1 = Show errors and warnings.
2 = Show errors, warnings, and SQL.
3 = Show errors, warnings, SQL, and complete controller dump.
App.baseUrl Un-comment this definition if you don’t plan to use Apache’s mod_rewrite with CakePHP. Don’t forget to remove your .htaccess files too.
Routing.admin Un-comment this definition if you’d like to take advantage of CakePHP admin routes. Set this variable to the name of the admin route you’d like to use. More on this later.
Cache.disable When set to true, caching is disabled site-wide.
Cache.check If set to true, enables view caching. Enabling is still needed in the controllers, but this variable enables the detection of those settings.
Session.save Tells CakePHP which session storage mechanism to use.

php = Use the default PHP session storage.
cake = Store session data in /app/tmp
database = store session data in a database table. Make sure to set up the table using the SQL file located at /app/config/sql/sessions.sql.
Session.table The name of the table (not including any prefix) that stores session information.
Session.database The name of the database that stores session information.
Session.cookie The name of the cookie used to track sessions.
Session.timeout Base session timeout in seconds. Actual value depends on Security.level.
Session.start Automatically starts sessions when set to true.
Session.checkAgent When set to false, CakePHP sessions will not check to ensure the user agent does not change between requests.
Security.level The level of CakePHP security. The session timeout time defined in 'Session.timeout' is multiplied according to the settings here.

Valid values:
'high' = x 10
'medium' = x 100
'low' = x 300

'high' and 'medium' also enable session.referer_check
Security.salt A random string used in security hashing.
Acl.classname, Acl.database Constants used for CakePHP’s Access Control List functionality. See the Access Control Lists chapter for more information.

Cache configuration is also found in core.php — We’ll be covering that later on, so stay tuned.

The Configure class can be used to read and write core configuration settings on the fly. This can be especially handy if you want to turn the debug setting on for a limited section of logic in your application, for instance.

3.4.3.3 Configuration Constants

While most configuration options are handled by Configure, there are a few constants that CakePHP uses during runtime.

Constant Description
LOG_ERROR Error constant. Used for differentiating error logging and debugging. Currently PHP supports LOG_DEBUG.
설정 클래스 :: 구성 :: CakePHP 개발 :: 매뉴얼 :: 1.2 Collection :: The Cookbook

매뉴얼

Table of Contents