Completed
Push — master ( b4d6a5...352f6d )
by Basil
02:42
created

Controller::getViewPath()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.4444
c 0
b 0
f 0
cc 8
nc 7
nop 0
1
<?php
2
3
namespace luya\web;
4
5
use luya\base\Module;
6
7
/**
8
 * Base class for all controllers in luya application Modules.
9
 *
10
 * @author Basil Suter <[email protected]>
11
 * @since 1.0.0
12
 */
13
abstract class Controller extends \yii\web\Controller
14
{
15
    /**
16
     * Helper method for registring an asset into the view.
17
     *
18
     * @param string $className The asset class to register, example `app\asset\MyTestAsset`.
19
     */
20
    public function registerAsset($className)
21
    {
22
        $className::register($this->view);
23
    }
24
25
    /**
26
     * Override the default Yii controller getViewPath method. To define the template folders in where
27
     * the templates are located. Why? Basically some modules needs to put theyr templates inside of the client
28
     * repository.
29
     *
30
     * @return string
31
     */
32
    public function getViewPath()
33
    {
34
        if ($this->module instanceof Module) {
35
            if ($this->module->useAppViewPath) {
36
                return '@app/views/' . $this->module->id . '/' . $this->id;
37
            } elseif (is_array($this->module->viewMap)) {
38
                $currentAction = $this->id . '/' . ($this->action ? $this->action->id : $this->defaultAction);
39
                foreach ($this->module->viewMap as $action => $viewPath) {
40
41
                    // Special case for map all views of controller
42
                    if ($action === '*') {
43
                        return $viewPath . '/' . $this->id;
44
                    } elseif (fnmatch($action, $currentAction)) {
45
                        return $viewPath;
46
                    }
47
                }
48
            }
49
        }
50
51
        return parent::getViewPath();
52
    }
53
54
    /**
55
     * If we are acting in the module context and the layout is empty we only should renderPartial the content.
56
     *
57
     * @param string $view   The name of the view file (e.g. index)
58
     * @param array  $params The params to assign into the value for key is the variable and value the content.
59
     *
60
     * @return string
61
     */
62
    public function render($view, $params = [])
63
    {
64
        if (!empty($this->module->context) && empty($this->layout)) {
65
            return $this->renderPartial($view, $params);
66
        }
67
68
        return parent::render($view, $params);
69
    }
70
71
    /**
72
     * Returns the path for layout files when using {{\luya\web\Controller::renderLayout()}} method. Those module layouts are located in @app/views folder.
73
     *
74
     * @return string The path to the layout for the current Module.
75
     */
76
    public function getModuleLayoutViewPath()
77
    {
78
        if ($this->module->useAppViewPath) {
0 ignored issues
show
Bug introduced by
The property useAppViewPath does not seem to exist. Did you mean viewPath?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
79
            return '@app/views/'.$this->module->id.'/';
80
        }
81
        
82
        return '@'.$this->module->id.'/views/';
83
    }
84
85
    /**
86
     * Luya implementation of layouts for controllers. The method will return a view file wrapped by a custom module layout.
87
     * For example you have a e-store module with a header which returns the basket you can use the module layout in all the actions
88
     * to retrieve the same header. Example e-store controller class:.
89
     *
90
     * ```php
91
     * class EstoreController extends \luya\base\Controller
92
     * {
93
     *     public function actionIndex()
94
     *     {
95
     *         return $this->renderLayout('index', ['content' => 'This is my index content in variabel $content.']);
96
     *     }
97
     *
98
     *     public function actionBasket()
99
     *     {
100
     *          return $this->renderLayout('basket', ['otherVariable' => 'This is a variable for the basket view file in variable $otherVariable.']);
101
     *     }
102
     * }
103
     * ```
104
     *
105
     * The example layout file which is located in `@app/views/module/layout` could look something like this:
106
     *
107
     * ```php
108
     * <ul>
109
     *  <li>E-Store Frontpage</li>
110
     *  <li>Basket</li>
111
     * </ul>
112
     * <div id="estore-wrapper">
113
     *      <?php echo $content; ?>
114
     * </div>
115
     * ```
116
     *
117
     * @param string $view   The name of the view file
118
     * @param array  $params The params to assign into the view file.
119
     *
120
     * @return string Rendered template wrapped by the module layout file.
121
     */
122
    public function renderLayout($view, $params = [])
123
    {
124
        $content = $this->view->render($view, $params, $this);
125
126
        return $this->render($this->getModuleLayoutViewPath().$this->module->moduleLayout, ['content' => $content]);
0 ignored issues
show
Bug introduced by
The property moduleLayout does not seem to exist. Did you mean layout?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
127
    }
128
}
129