Hi,
I'm using a simple model-view-controller structure and I'm really newbie at this. I'll show you the context:
My index.php
takes the url myweb.com/index.php?controller=access&action=login
and requires the base.php
to show the header and footer.
index.php:
if ( isset( $_GET['controller']) && isset( $_GET['action'] ) ) {
$controller = $_GET['controller'];
$action = $_GET['action'];
} else {
$controller = 'error';
$action = 'notfound';
}
spl_autoload_register();
require_once('app/base.phtml');
The base.php
puts header and footer html code, and instance a class called Router
that redirects the request depending on the controller and action of the url. Note that I'm using spl_autoload_register();
to autoload my classes.
app/ base.php:
<!-- header code here -->
use src\model\Router;
$router = new Router();
$router->callView($controller, $action);
<!-- footer code here -->
src/model/ Router.php:
namespace src\model;
class Router
{
function callView($controller, $action)
{
// code here that calls a controller to show a view
}
}
The thing is, when I require the Router
from base.php
, I get this error:
Fatal error: spl_autoload(): Class src\model\Router could not be loaded in /var/www/myweb/app/base.php on line 59
I thing my paths are correct, maybe I forget something. The autoloader works when I 'use' the 'namespaces' from other classes, but not when I use it from a simple php file.
The structure is the following:
myweb
|- app/
| |-- view/
| | |-- login.php
| |-- base.php
|
|- src/
| |-- controller/
| | |-- AccessController.php
|
|- model/
| |-- Router.php
|
|- index.php
|- .htaccess
PD:
I've modified the spl_autoload_register little bit
spl_autoload_register(
function($className) {
// echo "register: " . $className . "<br>\n";
$fileName = __DIR__ . '/' . str_replace('\\', '/', $className) . ".php";
// $fileName = __DIR__ . '\\' . $className . ".php"; // for windows
if(file_exists($fileName))
{
require_once($fileName);
}
else
{
echo "$fileName not found<br>\n";
}
}
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.