【问题标题】:Codeigniter4: routing goes to wrong routeCodeigniter4:路由转到错误的路由
【发布时间】:2022-01-03 07:03:16
【问题描述】:

在我的 codeigniter4 应用程序中,我遇到了奇怪的路由问题,

在路由配置中我有这样的组路由

$routes->group("admin", function($routes){
    $routes->match(['get'],'dashboard','Dashboard::index',[
        'as'=>'adm_dash'
    ]);
    //start customer
    $routes->get('customer/add','Customer::index',[
        'as'=>'adm_cust_add_new'
    ]);
    $routes->get('customer/view','Customer::view',[
        'as'=>'adm_cust_view'
    ]);
    $routes->get('customer/manage','Customer::manage',[
        'as'=>'adm_cust_manage'
    ]);
    
    //end customer

       //start customer actions
     $routes->post('customer/create','Customer::createcustomer',[
    'as'=>'adm_cust_create_action'
]);

    
});

在视图文件中

<form class="m-t-20" method="post" action="<?= route_to('adm_cust_create_action') ?>">
<button type="submit" class="btn btn-success waves-effect waves-light">Submit</button>
....
</form>

当我点击提交按钮时,路由转到admin/dashboard 路由而不是customer/create 路由

我的路由配置如下

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

【问题讨论】:

  • $routes-&gt;setPrioritize(false); 您可以随时在控制台查看您的路线:php spark routes

标签: codeigniter routes codeigniter-4 php-8.1


【解决方案1】:

restfull控制器和非restfull控制器的路由 看看代码对我来说很好

<?php

/*
 * Core Auth  routes file.
 */

$routes->group('api', ['namespace' => 'Modules\Auth\Controllers'], function ($routes) {

    $routes->resource('group', ['filter' => 'authJwt']);
    $routes->resource('permission', ['filter' => 'authJwt']);
    $routes->resource('groupPermission', ['filter' => 'authJwt']);
    $routes->resource('userPermission', ['filter' => 'authJwt']);

    $routes->group('auth', function ($routes) {

        $routes->post('signin-jwt', 'Auth::signInJwt', ['filter' => 'isSignIn']);
        $routes->post('signin', 'Auth::signIn', ['filter' => 'isSignIn']);
        $routes->get('signout', 'Auth::signOut', ['filter' => 'authJwt']);
        $routes->get('is-signin', 'Auth::isSignIn',['filter' => 'authJwt']);


        $routes->post('signup', 'Auth::signUp', ['filter' => 'isSignIn']);
        $routes->post('forgot', 'Auth::forgot', ['filter' => 'isSignIn']);

        $routes->post('reset-password-email', 'Auth::resetPasswordViaEmail', ['filter' => 'isSignIn']);
        $routes->post('reset-password-sms', 'Auth::resetPasswordViaSms', ['filter' => 'isSignIn']);

        $routes->post('activate-account-email', 'Auth::activateAccountViaEmail', ['filter' => 'isSignIn']);
        $routes->post('send-activate-email', 'Auth::sendActivateCodeViaEmail', ['filter' => 'isSignIn']);

        $routes->post('activate-account-sms', 'Auth::activateAccountViaSms', ['filter' => 'isSignIn']);
        $routes->post('send-activate-sms', 'Auth::sendActivateCodeViaSms', ['filter' => 'isSignIn']);

    });

});




【讨论】:

    猜你喜欢
    • 2021-09-25
    • 1970-01-01
    • 2017-09-02
    • 2017-09-25
    • 1970-01-01
    • 2015-08-15
    • 2020-06-28
    • 1970-01-01
    • 2015-11-24
    相关资源
    最近更新 更多