Codeigniter 4自动路由无法正常工作

6mw9ycah  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(338)

我一直试图按照从ci4网站教程如何启用自动路线,但它仍然不工作。有什么错误的代码吗?请帮助..
setautoroutes
not detected routes
improved autoroutes enable
Pages.php controller
我的控制器页面. php:

<?php

namespace App\Controllers;

use Config\View;

class Pages extends BaseController
{
    public function index()
    {
        $data = [
            'title' => 'Home | PETANI BEDAS'
        ];
        return view('pages/main', $data);
    }

    public function about()
    {
        $data = [
            'title' => 'About Us'
        ];
        return view('pages/about', $data);
    }

    public function contact()
    {
        $data = [
            'title' => 'contact',
            'alamat' => [
                [
                    'tempat' => 'Rumah',
                    'alamat' => 'Jl. Jendral Sudirman No. 54',
                    'kota' => 'Bandung'
                ],
                [
                    'tempat' => 'Kantor',
                    'alamat' => 'Jl. Gatot Subroto No. 14',
                    'kota' => 'Jakarta'
                ]
            ]
        ];
        return view('pages/contact', $data);
    }
}

我的配置路由. php:

<?php

namespace Config;

use App\Controllers\Coba;
use App\Controllers\Home;
use App\Controllers\Pages;
use CodeIgniter\Commands\Utilities\Routes;

// Create a new instance of our RouteCollection class.
$routes = Services::routes();

// Load the system's routing file first, so that the app and ENVIRONMENT
// can override as needed.
if (is_file(SYSTEMPATH . 'Config/Routes.php')) {
    require SYSTEMPATH . 'Config/Routes.php';
}

/*
 * --------------------------------------------------------------------
 * Router Setup
 * --------------------------------------------------------------------
 */
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);

// The Auto Routing (Legacy) is very dangerous. It is easy to create vulnerable apps
// where controller filters or CSRF protection are bypassed.
// If you don't want to define all routes, please use the Auto Routing (Improved).
// Set `$autoRoutesImproved` to true in `app/Config/Feature.php` and set the following to true.

/*
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */

// We get a performance increase by specifying the default
// route since we don't have to scan directories.

$routes->get('/', 'Pages::index');
// $routes->get('/pages/about', 'Pages::about');
// $routes->get('/pages/contact', 'Pages::contact');
// $routes->get('/pages/komik', 'Pages::komik');

/*
 * --------------------------------------------------------------------
 * Additional Routing
 * --------------------------------------------------------------------
 *
 * There will often be times that you need additional routing and you
 * need it to be able to override any defaults in this file. Environment
 * based routes is one such time. require() additional route files here
 * to make that happen.
 *
 * You will have access to the $routes object within that file without
 * needing to reload it.
 */
if (is_file(APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php')) {
    require APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php';
}

我的配置功能. php

<?php

namespace Config;

use CodeIgniter\Config\BaseConfig;

/**
 * Enable/disable backward compatibility breaking features.
 */
class Feature extends BaseConfig
{
    /**
     * Enable multiple filters for a route or not.
     *
     * If you enable this:
     *   - CodeIgniter\CodeIgniter::handleRequest() uses:
     *     - CodeIgniter\Filters\Filters::enableFilters(), instead of enableFilter()
     *   - CodeIgniter\CodeIgniter::tryToRouteIt() uses:
     *     - CodeIgniter\Router\Router::getFilters(), instead of getFilter()
     *   - CodeIgniter\Router\Router::handle() uses:
     *     - property $filtersInfo, instead of $filterInfo
     *     - CodeIgniter\Router\RouteCollection::getFiltersForRoute(), instead of getFilterForRoute()
     *
     * @var bool
     */
    public $multipleFilters = false;

    /**
     * Use improved new auto routing instead of the default legacy version.
     */
    public bool $autoRoutesImproved = true;
}
ffx8fchx

ffx8fchx1#

出于安全原因,如果在定义的路由中使用了controller,则“自动路由”不会路由到controller。
看看你在第二张照片中的错误!它不能访问(不是“找不到路线”)删除那些路线。
Codeigniter文档(阅读文档中的红框):链接到文档

相关问题