PHP URL pathinfo mode loading a simple implementation of different controllers _php instance

Source: Internet
Author: User

Using the parameters of automatically loading and parsing URLs, the implementation calls to different controllers, implementing the PathInfo mode and the normal URL pattern

File structure:

|--controller
|--index
|--index.php

|--application.php

application.php

<?php class application{public static function main () {header ("content-type:text/html;charset=utf-8");
    Self::register ();
  Self::router ();
  public static function register () {spl_autoload_register ("Self::loadclass");
    The public static function LoadClass ($class) {$class =str_replace (' \ \ ', '/', $class); $class = "./". $class. ".
    PHP ";    
  Require_once $class; public static function router () {if (Isset ($_server[' path_info ')) {$pathinfo =array_filter (Explode ("/", $_ser
      ver[' Path_info '));
        For ($i =1 $i <=count ($pathinfo), $i + +) {$key =isset ($pathinfo [$i])? $pathinfo [$i]: '; $value =isset ($pathinfo [$i +1])?
        $pathinfo [$i +1]: "";
            Switch ($i) {Case 1: $_get[' m ']=ucfirst ($key);
          Break
            Case 2: $_get[' C ']=ucfirst ($key);
          Break
            Case 3: $_get[' a ']= $key;
          Break Default:if ($i >3) {if ($i%2==0) {$_get[$key]= $value;
        }} break; }} $_get[' m ']=!empty ($_get[' m '))?
    Ucfirst ($_get[' m ']): ' Index '; $_get[' C ']=!empty ($_get[' C '])?
    Ucfirst ($_get[' C ']): ' Index '; $_get[' A ']=!empty ($_get[' a '])?
    $_get[' A ']: ' Index ';
    $class = "\\controller\\{$_get[' m ']}\\{$_get[' C ']}";
    $controller =new $class;
  $controller->$_get[' a '] (); } application::main ();

\controller\index\index.php

<?php
namespace Controller\index;

Use Service\user;
Class index{public
  function __construct () {
    echo construction method <br/>;
  }
  Public Function Index () {
    new User ();
    Print_r ($_get);
  }
  Public Function Login () {
    echo ' login () ';
  }
}

Effect:

Above this PHP URL's pathinfo mode load different controller's simple realization is small series to share to everybody's content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.

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.