回答編集履歴

2

fix typo

2015/11/06 17:39

投稿

gouf
gouf

スコア2321

test CHANGED
@@ -146,7 +146,7 @@
146
146
 
147
147
  <h1><?php echo $myvalue // => 'key1' ?></h1>
148
148
 
149
- <h2><?php echo test_method($myvalue) // => 'value1' ?></h1>
149
+ <h2><?php echo test_method($myvalue) // => 'value1' ?></h2>
150
150
 
151
151
  ```
152
152
 

1

具体的なコードを交えた回答に変更

2015/11/06 17:39

投稿

gouf
gouf

スコア2321

test CHANGED
@@ -1,40 +1,96 @@
1
- PHP やcodeigniter は詳しくわかりませんが:
1
+ PHP やcodeigniter は詳しくわかりませんが:
2
2
 
3
- * 設定を保存するメソッドを利用して、そちらにハッシュ値(Key : Value セット)を設定
3
+ * 設定を保存するメソッドを利用して、そちらにハッシュ値(Key : Value セット)を設定
4
4
 
5
- * 設定読み出しメソッドを呼んで 直接キー値を指定
5
+ * 設定読み出しメソッドを呼んで 直接キー値を指定
6
6
 
7
7
 
8
8
 
9
- できれば、explanation 関数もスッキリ書けるのではないでしょうか
9
+ できれば、explanation 関数もスッキリ書けるのではないでしょうか
10
+
11
+
12
+
13
+ 実際にプロジェクトを作成し、コードを書いてみました:
14
+
15
+
16
+
17
+ # プロジェクトの作成
18
+
19
+
20
+
21
+ ```bash
22
+
23
+ mkdir codeigniter
24
+
25
+ cd codeigniter
26
+
27
+ php composer.phar create-project bcit-ci/CodeIgniter .
28
+
29
+ ```
30
+
31
+
32
+
33
+ # 設定ファイルの作成と値の設定
34
+
35
+
36
+
37
+ application/config/my_config.php :
10
38
 
11
39
 
12
40
 
13
41
  ```php
14
42
 
15
- $config = array(
43
+ <?php
16
44
 
17
- 'key1' => 'value1',
45
+ $config['my_test_values'] = array(
18
46
 
19
- 'key2' => 'value2',
47
+ 'key1' => 'value1',
20
48
 
49
+ 'key2' => 'value2',
50
+
51
+ 'key3' => 'value3',
52
+
21
- // ....
53
+ 'key4' => 'value4'
22
54
 
23
55
  );
24
-
25
- $this->config->set_item('explanation', $config);
26
56
 
27
57
  ```
28
58
 
29
59
 
30
60
 
61
+ # ヘルパーの作成
62
+
63
+
64
+
65
+ application/helpers/message_helper.php :
66
+
31
67
  ```php
32
68
 
33
- function explanation($cond)
69
+ <?php
34
70
 
35
- {
71
+ if (!defined('BASEPATH')) exit('No direct script access allowed');
36
72
 
73
+
74
+
75
+ if (!function_exists('test_method')) {
76
+
77
+ function test_method($key = '')
78
+
79
+ {
80
+
81
+ $ci =& get_instance();
82
+
83
+ $ci->config->load('my_config'); // Load application/config/my_config.php
84
+
85
+
86
+
37
- return $this->config->item('explanation')['key1'];
87
+ $value = $ci->config->item('my_test_values'); // Get my_test_values configuration value
88
+
89
+
90
+
91
+ return $value[$key];
92
+
93
+ }
38
94
 
39
95
  }
40
96
 
@@ -42,12 +98,72 @@
42
98
 
43
99
 
44
100
 
45
- より詳しい人回答を待ちたいところですが、何か参考になれば幸いです
101
+ # コントローラでヘルパー読み込み
46
102
 
47
103
 
48
104
 
105
+ application/controllers/Welcome.php :
106
+
107
+ ```php
108
+
109
+ <?php
110
+
111
+ class Welcome extends CI_Controller
112
+
113
+ {
114
+
115
+ public function index()
116
+
117
+ {
118
+
119
+ $this->load->helper('message'); // Load application/helpers/message_helper.php
120
+
121
+
122
+
123
+ $data = array(
124
+
125
+ 'myvalue' => 'key1'
126
+
127
+ );
128
+
129
+ $this->load->view('welcome_message', $data);
130
+
131
+ }
132
+
133
+ }
134
+
135
+ ```
136
+
137
+
138
+
139
+ # ビューでヘルパーメソッドを呼び出して返り値を表示
140
+
141
+
142
+
143
+ application/views/welcome_message.php :
144
+
145
+ ```php
146
+
147
+ <h1><?php echo $myvalue // => 'key1' ?></h1>
148
+
149
+ <h2><?php echo test_method($myvalue) // => 'value1' ?></h1>
150
+
151
+ ```
152
+
153
+
154
+
155
+ 調べた情報を頼りに書いてみましたが、何か参考になれば幸いです
156
+
157
+
158
+
49
- # Link
159
+ # Links
50
160
 
51
161
 
52
162
 
53
163
  * [Config Class — CodeIgniter 3.0.3 documentation](https://codeigniter.com/user_guide/libraries/config.html?highlight=config)
164
+
165
+ * [php - Access database config variables from a helper in Codeigniter - Stack Overflow](http://stackoverflow.com/questions/7242413/access-database-config-variables-from-a-helper-in-codeigniter)
166
+
167
+ * [php - CodeIgniter: Create new helper? - Stack Overflow](http://stackoverflow.com/questions/804399/codeigniter-create-new-helper)
168
+
169
+ * [php - Passing variable from controller to view in CodeIgniter - Stack Overflow](http://stackoverflow.com/questions/12294527/passing-variable-from-controller-to-view-in-codeigniter)