How does mvc-php MVC implement classes that automatically load different namespaces?

Source: Internet
Author: User
Keywords PHP MVC
Tags autoload
Phpmvc

I would like to try to write a simple MVC framework, the results of the automatic loading is stuck, to seek the expert guidance
This is the directory structure

The code is as follows
Entry file index.php
Define (' BASEDIR ', __dir__);
Require BASEDIR. ' \autoload.php ';
Spl_autoload_register (' controllers\loader::autoload ');
$c = Strtolower ($_get[' C '); Controller name
$a = Strtolower ($_get[' a '); Method name
$controller = ' controllers\index\ '. $c. ' Controller '; This is the place where I want to go directly to new, without having to add a namespace in front of you and not know how to implement it.
$controller = $c. ' Controller ';
$obj = new $controller ();
$obj $a ();

Controller commonController.class.php:
namespace Controllers\common;
Class Commoncontroller {
/**

    • @param string $templets
    • @param array $var */public function display ($templets, $var) {define (' BASEDIR ', __dir__); Ob_clean (); Ob_start (); extract ($var); $templets = Str_replace ('/', ' \ ', $templets); $tmp _file = BASEDIR. ' \views\ '. $templets. HTML '; include_once $tmp _file;echo ob_get_contents ();}}

Controller indexController.class.php:
namespace Controllers\index;
Use Controllers\common\commoncontroller;
Class Indexcontroller extends commoncontroller{
Public Function index () {
$this->display (' Index/index ', [' Test ' = ' success ']);
}
}

Auto-load Class autoload.php:
namespace controllers;
Class loader{
static function AutoLoad ($className) {
$class = BASEDIR. ' \ '. $className. ' Class.php ';
$class = str_replace (' \ ', '/', $class);
if (file_exists ($class)) {
Include_once $class;
}
}
}
I would like to load the class directly in the portal file without needing to add a namespace in front of it, what to do, ask the big God for guidance under

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.