質問編集履歴
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -101,3 +101,565 @@
|
|
101
101
|
veiwファイルが何回も読み込まれる原因は何でしょうか。。?
|
102
102
|
|
103
103
|
SQLなどの問題ではないのは確認済みです。
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
追記
|
108
|
+
|
109
|
+
継承しているモデルです
|
110
|
+
|
111
|
+
```ここに言語を入力
|
112
|
+
|
113
|
+
abstract class ViewModel
|
114
|
+
|
115
|
+
{
|
116
|
+
|
117
|
+
/**
|
118
|
+
|
119
|
+
* Factory for fetching the ViewModel
|
120
|
+
|
121
|
+
*
|
122
|
+
|
123
|
+
* @param string ViewModel classname without View_ prefix or full classname
|
124
|
+
|
125
|
+
* @param string Method to execute
|
126
|
+
|
127
|
+
* @return ViewModel
|
128
|
+
|
129
|
+
*/
|
130
|
+
|
131
|
+
public static function forge($viewmodel, $method = 'view', $auto_filter = null, $view = null)
|
132
|
+
|
133
|
+
{
|
134
|
+
|
135
|
+
// if no custom view is given, make it equal to the viewmodel name
|
136
|
+
|
137
|
+
is_null($view) and $view = $viewmodel;
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
// strip any extensions from the view name to determine the viewmodel to load
|
142
|
+
|
143
|
+
$viewmodel = \Inflector::words_to_upper(str_replace(
|
144
|
+
|
145
|
+
array('/', DS),
|
146
|
+
|
147
|
+
'_',
|
148
|
+
|
149
|
+
strpos($viewmodel, '.') === false ? $viewmodel : substr($viewmodel, 0, -strlen(strrchr($viewmodel, '.')))
|
150
|
+
|
151
|
+
));
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
// determine the viewmodel namespace from the current request context
|
156
|
+
|
157
|
+
$namespace = \Request::active() ? ucfirst(\Request::active()->module) : '';
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
// list of possible viewmodel classnames, start with the namespaced one
|
162
|
+
|
163
|
+
$classes = array($namespace.'\\View_'.$viewmodel);
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
// add the global version if needed
|
168
|
+
|
169
|
+
empty($namespace) or $classes[] = 'View_'.$viewmodel;
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
/**
|
174
|
+
|
175
|
+
* Add non View_ prefixed classnames to the list, for BC reasons
|
176
|
+
|
177
|
+
*
|
178
|
+
|
179
|
+
* @deprecated 1.6
|
180
|
+
|
181
|
+
*/
|
182
|
+
|
183
|
+
$classes[] = $namespace.'\\'.$viewmodel;
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
// and add the global version of that if needed
|
188
|
+
|
189
|
+
empty($namespace) or $classes[] = $viewmodel;
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
// check if we can find one
|
194
|
+
|
195
|
+
foreach ($classes as $class)
|
196
|
+
|
197
|
+
{
|
198
|
+
|
199
|
+
if (class_exists($class))
|
200
|
+
|
201
|
+
{
|
202
|
+
|
203
|
+
return new $class($method, $auto_filter, $view);
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
throw new \OutOfBoundsException('ViewModel "'.reset($classes).'" could not be found.');
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
/**
|
218
|
+
|
219
|
+
* @var string method to execute when rendering
|
220
|
+
|
221
|
+
*/
|
222
|
+
|
223
|
+
protected $_method;
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
/**
|
228
|
+
|
229
|
+
* @var string|View view name, after instantiation a View object
|
230
|
+
|
231
|
+
*/
|
232
|
+
|
233
|
+
protected $_view;
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
/**
|
238
|
+
|
239
|
+
* @var bool whether or not to use auto filtering
|
240
|
+
|
241
|
+
*/
|
242
|
+
|
243
|
+
protected $_auto_filter;
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
/**
|
248
|
+
|
249
|
+
* @var Request active request during ViewModel creation for proper context
|
250
|
+
|
251
|
+
*/
|
252
|
+
|
253
|
+
protected $_active_request;
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
protected function __construct($method, $auto_filter = null, $view = null)
|
258
|
+
|
259
|
+
{
|
260
|
+
|
261
|
+
$this->_auto_filter = $auto_filter;
|
262
|
+
|
263
|
+
$this->_view === null and $this->_view = $view;
|
264
|
+
|
265
|
+
class_exists('Request', false) and $this->_active_request = \Request::active();
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
if (empty($this->_view))
|
270
|
+
|
271
|
+
{
|
272
|
+
|
273
|
+
// Take the class name and guess the view name
|
274
|
+
|
275
|
+
$class = get_class($this);
|
276
|
+
|
277
|
+
$this->_view = strtolower(str_replace('_', DS, preg_replace('#^([a-z0-9_]*\\\\)?(View_)?#i', '', $class)));
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
$this->set_view();
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
$this->_method = $method;
|
288
|
+
|
289
|
+
}
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
/**
|
294
|
+
|
295
|
+
* Returns the View object associated with this Viewmodel
|
296
|
+
|
297
|
+
*
|
298
|
+
|
299
|
+
* @return View
|
300
|
+
|
301
|
+
*/
|
302
|
+
|
303
|
+
public function get_view()
|
304
|
+
|
305
|
+
{
|
306
|
+
|
307
|
+
return $this->_view;
|
308
|
+
|
309
|
+
}
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
/**
|
314
|
+
|
315
|
+
* Construct the View object
|
316
|
+
|
317
|
+
*/
|
318
|
+
|
319
|
+
protected function set_view()
|
320
|
+
|
321
|
+
{
|
322
|
+
|
323
|
+
$this->_view instanceOf View or $this->_view = \View::forge($this->_view);
|
324
|
+
|
325
|
+
}
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
/**
|
330
|
+
|
331
|
+
* Returns the active request object.
|
332
|
+
|
333
|
+
*
|
334
|
+
|
335
|
+
* @return Request
|
336
|
+
|
337
|
+
*/
|
338
|
+
|
339
|
+
protected function request()
|
340
|
+
|
341
|
+
{
|
342
|
+
|
343
|
+
return $this->_active_request;
|
344
|
+
|
345
|
+
}
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
/**
|
350
|
+
|
351
|
+
* Executed before the view method
|
352
|
+
|
353
|
+
*/
|
354
|
+
|
355
|
+
public function before() {}
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
/**
|
360
|
+
|
361
|
+
* The default view method
|
362
|
+
|
363
|
+
* Should set all expected variables upon itself
|
364
|
+
|
365
|
+
*/
|
366
|
+
|
367
|
+
public function view() {}
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
/**
|
372
|
+
|
373
|
+
* Executed after the view method
|
374
|
+
|
375
|
+
*/
|
376
|
+
|
377
|
+
public function after() {}
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
/**
|
382
|
+
|
383
|
+
* Fetches an existing value from the template
|
384
|
+
|
385
|
+
*
|
386
|
+
|
387
|
+
* @return mixed
|
388
|
+
|
389
|
+
*/
|
390
|
+
|
391
|
+
public function & __get($name)
|
392
|
+
|
393
|
+
{
|
394
|
+
|
395
|
+
return $this->get($name);
|
396
|
+
|
397
|
+
}
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
/**
|
402
|
+
|
403
|
+
* Gets a variable from the template
|
404
|
+
|
405
|
+
*
|
406
|
+
|
407
|
+
* @param string
|
408
|
+
|
409
|
+
*/
|
410
|
+
|
411
|
+
public function & get($key = null, $default = null)
|
412
|
+
|
413
|
+
{
|
414
|
+
|
415
|
+
if (is_null($default) and func_num_args() === 1)
|
416
|
+
|
417
|
+
{
|
418
|
+
|
419
|
+
return $this->_view->get($key);
|
420
|
+
|
421
|
+
}
|
422
|
+
|
423
|
+
return $this->_view->get($key, $default);
|
424
|
+
|
425
|
+
}
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
/**
|
430
|
+
|
431
|
+
* Sets and sanitizes a variable on the template
|
432
|
+
|
433
|
+
*
|
434
|
+
|
435
|
+
* @param string
|
436
|
+
|
437
|
+
* @param mixed
|
438
|
+
|
439
|
+
*/
|
440
|
+
|
441
|
+
public function __set($key, $value)
|
442
|
+
|
443
|
+
{
|
444
|
+
|
445
|
+
return $this->set($key, $value);
|
446
|
+
|
447
|
+
}
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
/**
|
452
|
+
|
453
|
+
* Sets a variable on the template
|
454
|
+
|
455
|
+
*
|
456
|
+
|
457
|
+
* @param string
|
458
|
+
|
459
|
+
* @param mixed
|
460
|
+
|
461
|
+
* @param bool|null
|
462
|
+
|
463
|
+
*/
|
464
|
+
|
465
|
+
public function set($key, $value = null, $filter = null)
|
466
|
+
|
467
|
+
{
|
468
|
+
|
469
|
+
is_null($filter) and $filter = $this->_auto_filter;
|
470
|
+
|
471
|
+
$this->_view->set($key, $value, $filter);
|
472
|
+
|
473
|
+
|
474
|
+
|
475
|
+
return $this;
|
476
|
+
|
477
|
+
}
|
478
|
+
|
479
|
+
|
480
|
+
|
481
|
+
/**
|
482
|
+
|
483
|
+
* Magic method, determines if a variable is set.
|
484
|
+
|
485
|
+
*
|
486
|
+
|
487
|
+
* isset($view->foo);
|
488
|
+
|
489
|
+
*
|
490
|
+
|
491
|
+
* @param string variable name
|
492
|
+
|
493
|
+
* @return boolean
|
494
|
+
|
495
|
+
*/
|
496
|
+
|
497
|
+
public function __isset($key)
|
498
|
+
|
499
|
+
{
|
500
|
+
|
501
|
+
return isset($this->_view->$key);
|
502
|
+
|
503
|
+
}
|
504
|
+
|
505
|
+
|
506
|
+
|
507
|
+
/**
|
508
|
+
|
509
|
+
* Assigns a value by reference. The benefit of binding is that values can
|
510
|
+
|
511
|
+
* be altered without re-setting them. It is also possible to bind variables
|
512
|
+
|
513
|
+
* before they have values. Assigned values will be available as a
|
514
|
+
|
515
|
+
* variable within the view file:
|
516
|
+
|
517
|
+
*
|
518
|
+
|
519
|
+
* $this->bind('ref', $bar);
|
520
|
+
|
521
|
+
*
|
522
|
+
|
523
|
+
* @param string variable name
|
524
|
+
|
525
|
+
* @param mixed referenced variable
|
526
|
+
|
527
|
+
* @param bool Whether to filter the var on output
|
528
|
+
|
529
|
+
* @return $this
|
530
|
+
|
531
|
+
*/
|
532
|
+
|
533
|
+
public function bind($key, &$value, $filter = null)
|
534
|
+
|
535
|
+
{
|
536
|
+
|
537
|
+
$this->_view->bind($key, $value, $filter);
|
538
|
+
|
539
|
+
|
540
|
+
|
541
|
+
return $this;
|
542
|
+
|
543
|
+
}
|
544
|
+
|
545
|
+
|
546
|
+
|
547
|
+
/**
|
548
|
+
|
549
|
+
* Change auto filter setting
|
550
|
+
|
551
|
+
*
|
552
|
+
|
553
|
+
* @param null|bool change setting (bool) or get the current setting (null)
|
554
|
+
|
555
|
+
* @return void|bool returns current setting or nothing when it is changed
|
556
|
+
|
557
|
+
*/
|
558
|
+
|
559
|
+
public function auto_filter($setting = null)
|
560
|
+
|
561
|
+
{
|
562
|
+
|
563
|
+
if (func_num_args() == 0)
|
564
|
+
|
565
|
+
{
|
566
|
+
|
567
|
+
return $this->_view->auto_filter();
|
568
|
+
|
569
|
+
}
|
570
|
+
|
571
|
+
|
572
|
+
|
573
|
+
return $this->_view->auto_filter($setting);
|
574
|
+
|
575
|
+
}
|
576
|
+
|
577
|
+
|
578
|
+
|
579
|
+
|
580
|
+
|
581
|
+
/**
|
582
|
+
|
583
|
+
* Add variables through method and after() and create template as a string
|
584
|
+
|
585
|
+
*/
|
586
|
+
|
587
|
+
public function render()
|
588
|
+
|
589
|
+
{
|
590
|
+
|
591
|
+
if (class_exists('Request', false))
|
592
|
+
|
593
|
+
{
|
594
|
+
|
595
|
+
$current_request = Request::active();
|
596
|
+
|
597
|
+
Request::active($this->_active_request);
|
598
|
+
|
599
|
+
}
|
600
|
+
|
601
|
+
|
602
|
+
|
603
|
+
$this->before();
|
604
|
+
|
605
|
+
$this->{$this->_method}();
|
606
|
+
|
607
|
+
$this->after();
|
608
|
+
|
609
|
+
|
610
|
+
|
611
|
+
$return = $this->_view->render();
|
612
|
+
|
613
|
+
|
614
|
+
|
615
|
+
if (class_exists('Request', false))
|
616
|
+
|
617
|
+
{
|
618
|
+
|
619
|
+
Request::active($current_request);
|
620
|
+
|
621
|
+
}
|
622
|
+
|
623
|
+
|
624
|
+
|
625
|
+
return $return;
|
626
|
+
|
627
|
+
}
|
628
|
+
|
629
|
+
|
630
|
+
|
631
|
+
/**
|
632
|
+
|
633
|
+
* Auto-render on toString
|
634
|
+
|
635
|
+
*/
|
636
|
+
|
637
|
+
public function __toString()
|
638
|
+
|
639
|
+
{
|
640
|
+
|
641
|
+
try
|
642
|
+
|
643
|
+
{
|
644
|
+
|
645
|
+
return $this->render();
|
646
|
+
|
647
|
+
}
|
648
|
+
|
649
|
+
catch (\Exception $e)
|
650
|
+
|
651
|
+
{
|
652
|
+
|
653
|
+
\Error::exception_handler($e);
|
654
|
+
|
655
|
+
|
656
|
+
|
657
|
+
return '';
|
658
|
+
|
659
|
+
}
|
660
|
+
|
661
|
+
}
|
662
|
+
|
663
|
+
}
|
664
|
+
|
665
|
+
```
|