简体   繁体   English

如何在 codeigniter v4 的子目录中创建 controller?

[英]How to create a controller in a sub-directory in codeigniter v4?

In codeigniter v3 sub-directory in a controller is working fine.在 controller 中的 codeigniter v3 子目录中工作正常。 But in codeigniter v4 this is not working.但在 codeigniter v4 中,这不起作用。

Folder structure: Controllers/[sub-directory name]/Controller_name.php文件夹结构: Controllers/[子目录名]/Controller_name.php

Need this Url: htp://domain.com/[sub-directory name]/controller_name/需要这个 Url: htp://domain.com/[子目录名]/controller_name/

How can I solve this in Codeigniter v4?如何在 Codeigniter v4 中解决这个问题?

If you want to use the BaseController you have to specify "which one", using USE or you can explicitly specify it each time you use an object.如果您想使用 BaseController,您必须指定“哪个”,使用 USE 或者您可以在每次使用 object 时明确指定它。

So you could have ( as deste had answered ) but using BaseController所以你可以(正如 deste 回答的那样)但使用 BaseController

<?php namespace App\Controllers\Ajax;

    class Test extends \App\Controller\BaseController // Which BaseController
    {
        public function index()
        {
            return 'controller works!';
        }
    }

OR the nicer way using "use" is to specify exactly which BaseController you want to use once.或者使用“使用”的更好方法是准确指定您想要使用的 BaseController 一次。

<?php namespace App\Controllers\Ajax;

use App\Controllers\BaseController; // Which BaseController are you referring to.

class Test extends BaseController
{
    public function index()
    {
        return 'controller works!';
    }
}

It's a matter of telling PHP Where your files are.只需告诉 PHP 您的文件在哪里。

Not the clearest of explanations but think of it as you have to say "where something is".不是最清楚的解释,但请考虑一下,因为您必须说“某物在哪里”。

I strongly recommend reading over the Codeigniter User Guide a few more times and messing around with it more to get the hang of how it all works.我强烈建议您多阅读几次 Codeigniter 用户指南,并多多研究以了解它是如何工作的。 You might also like to read Primer: Namespaces & CodeIgniter 4 by Lonnie Ezell (one of CodeIgniter's developers).您可能还想阅读Lonnie Ezell (CodeIgniter 的开发人员之一)的Primer: Namespaces & CodeIgniter 4

Using "Namespaces" and "use" you can create HMVC structures or anything you like.使用“命名空间”和“使用”,您可以创建 HMVC 结构或任何您喜欢的东西。 They are powerful and once you get the basics, very simple.它们功能强大,一旦您掌握了基础知识,就非常简单。

I am not so familiar yet with CodeIgniter 4 and namespaces... But I suppose that it's a namespace issue.我对 CodeIgniter 4 和命名空间还不太熟悉……但我想这是一个命名空间问题。

I have tried to replicate your case.我试图复制你的案例。 I created a directory./App/Controllers/ajax and inside this a Test.php file:我创建了一个目录。/App/Controllers/ajax 并在其中创建了一个 Test.php 文件:

<?php namespace App\Controllers\Ajax;

class Test extends \CodeIgniter\Controller
{
    public function index()
    {
        return 'controller works!';
    }
}

https://myserver/ajax/test page work fine. https://myserver/ajax/test页面工作正常。 I hope this can help you in some way.我希望这可以在某种程度上帮助你。

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

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