Bootstrap
Vorige week ben ik weer begonnen met het leren maken van een website d.m.v. MVC en daar is de volgende bootstrap uitgekomen:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
// Class Bootstrap
class Bootstrap
{
// Initialize variables
private $_controller;
// Constructor
public function __construct()
{
// Explode url
$_url = explode('/', $_GET['url']);
// Check if url[0] is empty
if (empty($_url[0]))
{
// Require controller
require PATH_ROOT . '/controllers/index.php';
// Create controller
$this->_controller = new IndexController();
$this->_controller->index();
}
else
{
// Initialize variables
$_file = PATH_ROOT . '/controllers/' . $_url[0] . '.php';
$_class = $_url[0] . 'Controller';
$_arguments = array_slice($_url, 2);
// Check if file exists
if (file_exists($_file))
{
// Require controller
require $_file;
// Create controller
$this->_controller = new $_class;
$this->_controller->loadModel($_url[0]);
// Check if method exists
if (method_exists($this->_controller, $_url[1]))
{
// Execute method
$this->_controller->{$_url[1]}($_arguments);
}
else
{
// Execute method
$this->_controller->index($_arguments);
}
}
else
{
// Require controller
require PATH_ROOT . '/controllers/error.php';
// Create controller
$this->_controller = new ErrorController();
$this->_controller->index($_arguments);
}
}
}
}
?>
// Class Bootstrap
class Bootstrap
{
// Initialize variables
private $_controller;
// Constructor
public function __construct()
{
// Explode url
$_url = explode('/', $_GET['url']);
// Check if url[0] is empty
if (empty($_url[0]))
{
// Require controller
require PATH_ROOT . '/controllers/index.php';
// Create controller
$this->_controller = new IndexController();
$this->_controller->index();
}
else
{
// Initialize variables
$_file = PATH_ROOT . '/controllers/' . $_url[0] . '.php';
$_class = $_url[0] . 'Controller';
$_arguments = array_slice($_url, 2);
// Check if file exists
if (file_exists($_file))
{
// Require controller
require $_file;
// Create controller
$this->_controller = new $_class;
$this->_controller->loadModel($_url[0]);
// Check if method exists
if (method_exists($this->_controller, $_url[1]))
{
// Execute method
$this->_controller->{$_url[1]}($_arguments);
}
else
{
// Execute method
$this->_controller->index($_arguments);
}
}
else
{
// Require controller
require PATH_ROOT . '/controllers/error.php';
// Create controller
$this->_controller = new ErrorController();
$this->_controller->index($_arguments);
}
}
}
}
?>
Het werkt zoals ik wil, maar ik heb geen idee of ik dit op een juiste manier heb gedaan, hebben jullie tips wat ik er beter aan kan?
Alvast bedankt!
Je hebt wel een method_exists() check, maar geen class_exists() na het requireën.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
// Class Bootstrap
class Bootstrap
{
// Initialize variables
private $_controller;
private $_url;
// Constructor
public function __construct($_url)
{
// Explode url
$this->_url = explode('/', $_url);
// Check if url[0] is empty
if (empty($this->_url[0]))
{
// Require controller
require PATH_ROOT . '/controllers/index.php';
// Create controller
$this->_controller = new IndexController();
$this->_controller->index();
}
else
{
// Initialize variables
$_file = PATH_ROOT . '/controllers/' . $this->_url[0] . '.php';
$_class = $this->_url[0] . 'Controller';
$_arguments = array_slice($this->_url, 2);
// Check if file exists
if (file_exists($_file))
{
// Require controller
require $_file;
// Check if class exists
if (class_exists($_class))
{
// Create controller
$this->_controller = new $_class;
$this->_controller->loadModel($this->_url[0]);
// Check if method exists
if (method_exists($this->_controller, $this->_url[1]))
{
// Execute method
$this->_controller->{$this->_url[1]}($_arguments);
}
else
{
// Execute method
$this->_controller->index($_arguments);
}
}
else
{
// Require controller
require PATH_ROOT . '/controllers/error.php';
// Create controller
$this->_controller = new ErrorController();
$this->_controller->index($_arguments);
}
}
else
{
// Require controller
require PATH_ROOT . '/controllers/error.php';
// Create controller
$this->_controller = new ErrorController();
$this->_controller->index($_arguments);
}
}
}
}
?>
// Class Bootstrap
class Bootstrap
{
// Initialize variables
private $_controller;
private $_url;
// Constructor
public function __construct($_url)
{
// Explode url
$this->_url = explode('/', $_url);
// Check if url[0] is empty
if (empty($this->_url[0]))
{
// Require controller
require PATH_ROOT . '/controllers/index.php';
// Create controller
$this->_controller = new IndexController();
$this->_controller->index();
}
else
{
// Initialize variables
$_file = PATH_ROOT . '/controllers/' . $this->_url[0] . '.php';
$_class = $this->_url[0] . 'Controller';
$_arguments = array_slice($this->_url, 2);
// Check if file exists
if (file_exists($_file))
{
// Require controller
require $_file;
// Check if class exists
if (class_exists($_class))
{
// Create controller
$this->_controller = new $_class;
$this->_controller->loadModel($this->_url[0]);
// Check if method exists
if (method_exists($this->_controller, $this->_url[1]))
{
// Execute method
$this->_controller->{$this->_url[1]}($_arguments);
}
else
{
// Execute method
$this->_controller->index($_arguments);
}
}
else
{
// Require controller
require PATH_ROOT . '/controllers/error.php';
// Create controller
$this->_controller = new ErrorController();
$this->_controller->index($_arguments);
}
}
else
{
// Require controller
require PATH_ROOT . '/controllers/error.php';
// Create controller
$this->_controller = new ErrorController();
$this->_controller->index($_arguments);
}
}
}
}
?>
Klopt dit? En kan ik dit ook nog onderverdelen in meerdere (private) methods? Ik gebruik bijvoorbeeld 2x dit stukje:
Code (php)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
<?php
// Require controller
require PATH_ROOT . '/controllers/error.php';
// Create controller
$this->_controller = new ErrorController();
$this->_controller->index($_arguments);
?>
// Require controller
require PATH_ROOT . '/controllers/error.php';
// Create controller
$this->_controller = new ErrorController();
$this->_controller->index($_arguments);
?>
Zou ik hier bijvoorbeeld 'private function _error()' van kunnen maken?
Gewijzigd op 16/10/2013 16:39:59 door Lord Gaga
Dat zou kunnen, wat ook kan is aan het einde een losstaande if ( ! $this->_controller) { ... } te zetten.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
// Class Bootstrap
class Bootstrap
{
// Initialize variables
private $_controller;
private $_url;
// Constructor
public function __construct($_url)
{
// Explode url
$this->_url = explode('/', $_url);
// Check if url[0] is empty
if (empty($this->_url[0]))
{
// Require controller
require PATH_ROOT . '/controllers/index.php';
// Create controller
$this->_controller = new IndexController();
$this->_controller->index();
}
else
{
// Initialize variables
$_file = PATH_ROOT . '/controllers/' . $this->_url[0] . '.php';
$_class = $this->_url[0] . 'Controller';
$_arguments = array_slice($this->_url, 2);
// Check if file exists
if (file_exists($_file))
{
// Require controller
require $_file;
// Check if class exists
if (class_exists($_class))
{
// Create controller
$this->_controller = new $_class;
$this->_controller->loadModel($this->_url[0]);
// Check if method exists
if (method_exists($this->_controller, $this->_url[1]))
{
// Execute method
$this->_controller->{$this->_url[1]}($_arguments);
}
else
{
// Execute method
$this->_controller->index($_arguments);
}
}
}
}
// If no controller is set
if (!($this->_controller))
{
// Require controller
require PATH_ROOT . '/controllers/error.php';
// Create controller
$this->_controller = new ErrorController();
$this->_controller->index($_arguments);
}
}
}
?>
// Class Bootstrap
class Bootstrap
{
// Initialize variables
private $_controller;
private $_url;
// Constructor
public function __construct($_url)
{
// Explode url
$this->_url = explode('/', $_url);
// Check if url[0] is empty
if (empty($this->_url[0]))
{
// Require controller
require PATH_ROOT . '/controllers/index.php';
// Create controller
$this->_controller = new IndexController();
$this->_controller->index();
}
else
{
// Initialize variables
$_file = PATH_ROOT . '/controllers/' . $this->_url[0] . '.php';
$_class = $this->_url[0] . 'Controller';
$_arguments = array_slice($this->_url, 2);
// Check if file exists
if (file_exists($_file))
{
// Require controller
require $_file;
// Check if class exists
if (class_exists($_class))
{
// Create controller
$this->_controller = new $_class;
$this->_controller->loadModel($this->_url[0]);
// Check if method exists
if (method_exists($this->_controller, $this->_url[1]))
{
// Execute method
$this->_controller->{$this->_url[1]}($_arguments);
}
else
{
// Execute method
$this->_controller->index($_arguments);
}
}
}
}
// If no controller is set
if (!($this->_controller))
{
// Require controller
require PATH_ROOT . '/controllers/error.php';
// Create controller
$this->_controller = new ErrorController();
$this->_controller->index($_arguments);
}
}
}
?>