简体   繁体   English

将家庭控制器映射为 AltoRouter 中的默认控制器

[英]Mapping home controller as the default controller in AltoRouter

This is the index.php这是index.php

<?php
include 'library/altorouter.php';

$router = new AltoRouter();
$router->setBasePath('/AltoRouter'); 

$router->map('GET','/', 'home_controller#index', 'home');
$router->map('GET','/content/[:parent]/?[:child]?', 'content_controller#display_item', 'content');

$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();

     var_dump($obj);

    call_user_func_array(array($obj,$action), array($match['params']));

} else if ($match['target']==''){
    echo 'Error: no route was matched'; 

} else {
    echo 'Error: can not call '.$controller.'#'.$action; 

}

// content_controller class file is autoloaded 

class home_controller {
    public function index() {
        echo 'hi from home';
    }
}

and it works good.它运作良好。 The home_controller class is supposed to be the default controller. home_controller 类应该是默认控制器。

Problem is, when I remove the class home_controller问题是,当我删除类 home_controller

class home_controller {
    public function index() {
        echo 'hi from home';
    }
}

and save it as a seprate file home_controller.php in app/controller directroy it does not work.并将其另存为app/controller目录中的单独文件home_controller.php它不起作用。

I understand that the router is unable to locate the home_controller class hence will not show it's content (if i directly include the file home_controller.php it again works as normal).我知道路由器无法定位 home_controller 类,因此不会显示它的内容(如果我直接包含文件 home_controller.php 它再次正常工作)。

My question is, how do you map the home_controller as default, which is in a different directory?我的问题是,您如何将 home_controller 映射为默认值,它位于不同的目录中?

It looks like you're not using composer for installing the package.看起来您没有使用Composer来安装软件包。 It's standard way in PHP.这是 PHP 中的标准方式。


1. Install Composer 1. 安装作曲家


2. Call Composer from Command Line 2. 从命令行调用 Composer

Go to your root directory of your project, open Command Line and type:转到项目的根目录,打开命令行并键入:

composer require altorouter/altorouter

You'll find the package name altorouter/altorouter in composer.json on Github page of package - here .你会发现包名altorouter/altoroutercomposer.json包Github的页面上- 在这里


3. Add loaded files to your index.php 3. 将加载的文件添加到您的index.php

Now you have installed router package.现在您已经安装了路由器包。 Next step is adding all composer loaded files to your application.下一步是将所有 Composer 加载的文件添加到您的应用程序中。 Just replace include 'library/altorouter.php';只需替换include 'library/altorouter.php'; with following:具有以下内容:

<?php

# index.php

require_once __DIR__ . '/vendor/autoload.php';


4. Load Your Controllers by Composer as well 4. 也可以通过 Composer 加载您的控制器

Last step is tell composer where to find your classes .最后一步是告诉作曲家在哪里可以找到您的课程

Open composer.json and add following section:打开composer.json并添加以下部分:

{
    "autolaod": {
        "classmap": ["app"]
    }
}

Read more about classmap option in documentation .在文档中阅读有关classmap选项的更多信息。

To update /vendor/autoload.php with this option, just call from command line:要使用此选项更新/vendor/autoload.php ,只需从命令行调用:

 composer dump-autoload

That should be it.应该是这样。 If you come at any troubles, let me know which point.如果您遇到任何麻烦,请告诉我哪一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM