66
77trait ControllerAdditions
88{
9-
109 /**
1110 * @param \Illuminate\View\View|string|array $view View object or view path
1211 * @param array $data Data that should be made available to the view. Only used when $view is a string path
13- * @param string|boolean $layout Master layout path
12+ * @param mixed $options If $options is a string, it's used as the master layout path of the view.
13+ * E.G. $this->render('messages.index', 'app'); where the layout 'app' is the 'resources/views/app.blade.php' file.
14+ * If $options is null, the master layout is set with the value of the Controller::$layout property.
15+ * If $options is a boolean, if`it's `false`, no layout is applied on the view.
16+ * If $options is an array, default values are: ['status' => 200, 'headers' => []]
1417 *
1518 * @return \Illuminate\Http\Response
1619 */
17- public function render ($ view , $ data = [], $ layout = null )
20+ public function render ($ view , $ data = [], $ options = null )
1821 {
19- $ format = null ;
22+ $ layout = null ;
23+ $ defaultOptions = ['status ' => 200 , 'headers ' => []];
24+ if (is_string ($ options )) {
25+ // To support legacy behaviour
26+ $ layout = $ options ;
27+ $ options = [];
28+ } elseif (is_array ($ options )) {
29+ if (array_key_exists ('layout ' , $ options )) {
30+ $ layout = $ options ['layout ' ];
31+ unset($ options ['layout ' ]);
32+ }
33+ } else {
34+ if ($ options === false ) {
35+ // To support legacy behaviour
36+ $ layout = false ;
37+ }
38+ $ options = [];
39+ }
40+ $ options = array_merge ($ defaultOptions , $ options );
2041
42+ $ format = null ; // if `null`, it uses the 'html' format by default
2143 if (is_array ($ view ) && count ($ view ) === 1 ) {
2244 $ format = array_keys ($ view )[0 ];
2345 $ view = array_values ($ view )[0 ];
@@ -32,12 +54,13 @@ public function render($view, $data = [], $layout = null)
3254 }
3355
3456 if (is_string ($ view ) && view ()->exists ($ view )) {
57+ // Transform the $view string path to a View object
3558 $ view = view ($ view , $ data );
3659 }
3760
3861 // short circuit
3962 if ($ format ) {
40- $ response = response ($ view );
63+ $ response = response ($ view, $ options [ ' status ' ], $ options [ ' headers ' ] );
4164 $ response ->header ('Content-Type ' , Request::getMimeType ($ format ));
4265
4366 return $ response ;
@@ -48,13 +71,34 @@ public function render($view, $data = [], $layout = null)
4871 $ this ->setupLayout ();
4972 }
5073
74+ $ render = $ view ;
5175 if ($ this ->layout ) {
5276 $ this ->layout ->content = $ view ;
77+ $ render = $ this ->layout ;
78+ }
79+
80+ if (response ()->hasMacro ('makeWithTurbolinks ' )) {
81+ return response ()->makeWithTurbolinks ($ render , $ options );
82+ }
5383
54- return response ($ this ->layout );
84+ return response ()->make ($ render , $ options ['status ' ], $ options ['headers ' ]);
85+ }
86+
87+ /**
88+ * @param string $path URL
89+ * @param array $options Default to: ['status' => 302, 'headers' => [], 'secure' => null]
90+ *
91+ * @return \Illuminate\Http\RedirectResponse
92+ */
93+ public function redirectTo ($ path , $ options = [])
94+ {
95+ $ defaultOptions = ['status ' => 302 , 'headers ' => [], 'secure ' => null ];
96+ $ options = array_merge ($ defaultOptions , $ options );
97+ if (response ()->hasMacro ('redirectToWithTurbolinks ' )) {
98+ return response ()->redirectToWithTurbolinks ($ path , $ options );
5599 }
56100
57- return response ($ view );
101+ return response ()-> redirectTo ( $ path , $ options [ ' status ' ], $ options [ ' headers ' ], $ options [ ' secure ' ] );
58102 }
59103
60104 /**
0 commit comments