Skip to content

Commit c0ac83d

Browse files
Adding base Controller
1 parent bd74cfb commit c0ac83d

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed

src/Bases/Controller.php

+179
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?php namespace Arcanedev\Support\Bases;
2+
3+
use Illuminate\Routing\Controller as IlluminateController;
4+
5+
/**
6+
* Class Controller
7+
*
8+
* @package Arcanedev\Support\Bases
9+
* @author ARCANEDEV <arcanedev.maroc@gmail.com>
10+
*/
11+
abstract class Controller extends IlluminateController
12+
{
13+
/* ------------------------------------------------------------------------------------------------
14+
| Traits
15+
| ------------------------------------------------------------------------------------------------
16+
*/
17+
use \Arcanedev\Support\Traits\AbortTrait,
18+
\Illuminate\Foundation\Auth\Access\AuthorizesRequests,
19+
\Illuminate\Foundation\Bus\DispatchesJobs,
20+
\Illuminate\Foundation\Validation\ValidatesRequests;
21+
22+
/* ------------------------------------------------------------------------------------------------
23+
| Properties
24+
| ------------------------------------------------------------------------------------------------
25+
*/
26+
/**
27+
* The view template - master, layout (... whatever).
28+
*
29+
* @var string
30+
*/
31+
protected $template = '_templates.default.master';
32+
33+
/**
34+
* The layout view.
35+
*
36+
* @var \Illuminate\View\View
37+
*/
38+
private $layout;
39+
40+
/**
41+
* The view data.
42+
*
43+
* @var array
44+
*/
45+
protected $data = [];
46+
47+
/* ------------------------------------------------------------------------------------------------
48+
| Constructor
49+
| ------------------------------------------------------------------------------------------------
50+
*/
51+
/**
52+
* Instantiate the controller.
53+
*/
54+
public function __construct()
55+
{
56+
$this->setData('page', '');
57+
}
58+
59+
/* ------------------------------------------------------------------------------------------------
60+
| Illuminate Functions
61+
| ------------------------------------------------------------------------------------------------
62+
*/
63+
/**
64+
* Execute an action on the controller.
65+
*
66+
* @param string $method
67+
* @param array $parameters
68+
*
69+
* @return \Symfony\Component\HttpFoundation\Response
70+
*/
71+
public function callAction($method, $parameters)
72+
{
73+
$this->setupLayout();
74+
75+
return parent::callAction($method, $parameters);
76+
}
77+
78+
/**
79+
* Setup the template/layout.
80+
*/
81+
protected function setupLayout()
82+
{
83+
if (is_null($this->template)) {
84+
abort(500, 'The layout is not set');
85+
}
86+
87+
if ( ! $this->checkViewExists($this->template)) {
88+
abort(500, 'The layout [' . $this->template . '] not found');
89+
}
90+
91+
$this->layout = view($this->template);
92+
}
93+
94+
/* ------------------------------------------------------------------------------------------------
95+
| Getters & Setters
96+
| ------------------------------------------------------------------------------------------------
97+
*/
98+
/**
99+
* Set view data.
100+
*
101+
* @param string|array $name
102+
* @param mixed $value
103+
*
104+
* @return self
105+
*/
106+
protected function setData($name, $value = null)
107+
{
108+
if (is_array($name)) {
109+
$this->data = array_merge($this->data, $name);
110+
}
111+
elseif (is_string($name)) {
112+
$this->data[$name] = $value;
113+
}
114+
115+
return $this;
116+
}
117+
118+
/* ------------------------------------------------------------------------------------------------
119+
| Main Functions
120+
| ------------------------------------------------------------------------------------------------
121+
*/
122+
/**
123+
* Display the view.
124+
*
125+
* @param string $view
126+
*
127+
* @return \Illuminate\View\View
128+
*/
129+
protected function view($view)
130+
{
131+
if ( ! $this->checkViewExists($view)) {
132+
abort(500, 'The view [' . $view . '] not found');
133+
}
134+
135+
return $this->layout->with($this->data)->nest('content', $view, $this->data);
136+
}
137+
138+
/* ------------------------------------------------------------------------------------------------
139+
| Check Functions
140+
| ------------------------------------------------------------------------------------------------
141+
*/
142+
/**
143+
* Check if view exists.
144+
*
145+
* @param string $view
146+
*
147+
* @return bool
148+
*/
149+
protected function checkViewExists($view)
150+
{
151+
/** @var \Illuminate\View\Factory $viewFactory */
152+
$viewFactory = view();
153+
154+
return $viewFactory->exists($view);
155+
}
156+
157+
/**
158+
* Check if the Request is an ajax Request.
159+
*
160+
* @return bool
161+
*/
162+
protected static function isAjaxRequest()
163+
{
164+
return request()->ajax();
165+
}
166+
167+
/**
168+
* Accepts only ajax request.
169+
*
170+
* @param string $message
171+
* @param array $headers
172+
*/
173+
protected static function onlyAjax($message = 'Access denied !', array $headers = [])
174+
{
175+
if ( ! self::isAjaxRequest()) {
176+
self::accessNotAllowed($message, $headers);
177+
}
178+
}
179+
}

0 commit comments

Comments
 (0)