[SPL] autoloader
Ik maak gebruik van de spl autoloader functie alleen heb ik er nu problemen mee.
Kan nergens vinden hoe ik de autoloader function kan "stop" zetten.
Weet iemand of dit wel mogelijk is ?
Gr wouter.
In principe mag je een autoloader niet stopzetten.
Sterker nog: een autoloader mag eigenlijk niet eens fouten genereren.
Want normaal als je require gebruikt zou je inderdaad geen fouten moeten krijgen.
hier is mijn autoloader :
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
run_autoloader(SYSDIR.HELPPATH,"dmz_autoloader"); // Functie die de autoloader aanroept
{autoloader}
if(!function_exists('dmz_autoloader')) {
function dmz_autoloader($class){
require_once SYSDIR . COREPATH . $class . '.php';
}
spl_autoload_register('dmz_autoloader');
}
?>
run_autoloader(SYSDIR.HELPPATH,"dmz_autoloader"); // Functie die de autoloader aanroept
{autoloader}
if(!function_exists('dmz_autoloader')) {
function dmz_autoloader($class){
require_once SYSDIR . COREPATH . $class . '.php';
}
spl_autoload_register('dmz_autoloader');
}
?>
Hier de code waardoor het misgaat :
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
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
<?php
require SYSDIR."vendor/altorouter/AltoRouter/AltoRouter.php";
$router = new AltoRouter();
#$router->setBasePath('http://test.domeinz.nl');
foreach($routes as $key => $route) {
#echo '$router->map('. $route[0] .",". $route[1] .",". $route[2] .",". $key .')<br />';
$router->map(
$route[0]
, $route[1]
, $route[2]
, $key
);
}
// match current request
$match = $router->match();
// not sure if code after this comment is the best way to handle matched routes
list( $controller, $action ) = explode( '#', $match['target'] );
if ( is_callable(array($controller, $action)) ) {
$obj = new $controller();
call_user_func_array(array($obj,$action), array($match['params']));
} else if ($match['target']==''){
echo 'Error: no route was matched';
//possibly throw a 404 error
} else {
echo 'Error: can not call '.$controller.'#'.$action;
//possibly throw a 404 error
}
?>
require SYSDIR."vendor/altorouter/AltoRouter/AltoRouter.php";
$router = new AltoRouter();
#$router->setBasePath('http://test.domeinz.nl');
foreach($routes as $key => $route) {
#echo '$router->map('. $route[0] .",". $route[1] .",". $route[2] .",". $key .')<br />';
$router->map(
$route[0]
, $route[1]
, $route[2]
, $key
);
}
// match current request
$match = $router->match();
// not sure if code after this comment is the best way to handle matched routes
list( $controller, $action ) = explode( '#', $match['target'] );
if ( is_callable(array($controller, $action)) ) {
$obj = new $controller();
call_user_func_array(array($obj,$action), array($match['params']));
} else if ($match['target']==''){
echo 'Error: no route was matched';
//possibly throw a 404 error
} else {
echo 'Error: can not call '.$controller.'#'.$action;
//possibly throw a 404 error
}
?>
en de error :
Warning: include(system/core/Home.php): failed to open stream: No such file or directory in domeinz.nl/httpd.www/test/system/helpers/dmz_autoloader.php on line 16 Warning: include(): Failed opening 'system/core/Home.php' for inclusion (include_path='.:/usr/share/php') in domeinz.nl/httpd.www/test/system/helpers/dmz_autoloader.php on line 16 Error: can not call Home#index
Wat je daarom wel moet doen, is zelf even controleren of het pad in je autoloader klopt. Volgens de "No such file or directory" klopt dat namelijk niet.
Heb zojuist een andere oplossing gevonden.
Kan namelijk meerdere loaders gebruiken en heb de hoofdfunctie de volgende code gegeven :
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
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
<?php
if( ! function_exists('run_autoloader'))
{
/**
* Start the basic definition for the autoloader class.
* We need to define all the paths for locating and loading, and a bunch of other stuff.
* @return nothing :)
*/
function run_autoloader($autoloader = null) {
$ext = ".php";
spl_autoload_extensions(".php"); // comma-separated list
set_include_path(
implode(
PATH_SEPARATOR,
array(
get_include_path(),
'./app/controllers',
'./app/models',
'./system/core'
)
)
);
if(is_readable(SYSDIR.HELPPATH.$autoloader.$ext)) {
include SYSDIR.HELPPATH.$autoloader.$ext;
spl_autoload_register($autoloader);
}
}
}
?>
if( ! function_exists('run_autoloader'))
{
/**
* Start the basic definition for the autoloader class.
* We need to define all the paths for locating and loading, and a bunch of other stuff.
* @return nothing :)
*/
function run_autoloader($autoloader = null) {
$ext = ".php";
spl_autoload_extensions(".php"); // comma-separated list
set_include_path(
implode(
PATH_SEPARATOR,
array(
get_include_path(),
'./app/controllers',
'./app/models',
'./system/core'
)
)
);
if(is_readable(SYSDIR.HELPPATH.$autoloader.$ext)) {
include SYSDIR.HELPPATH.$autoloader.$ext;
spl_autoload_register($autoloader);
}
}
}
?>
zal aan de andere help loaders de file_exist() en is_file() functies toevoegen !