codeigniter 代码触发器4:如何启用用户定义的路由?

cgfeq70w  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(143)

我只是想知道如何在使用UpStudProf函数后重定向到StudProfile。运行UpStudProf函数后,URL变成了http://localhost/csms/public/index.php/Home/StudProfile,但它应该是http://localhost/Home/StudProfile,是否可以删除URL上的Controller名称Home

public function StudProfile(){

$crudModel = new Mod_Stud();
$data = [];
$data['user_data'] = $crudModel->orderBy('s_id', 'ASC')->findAll();
$data['title']      = 'SMS | STUDENT PROFILE';
$data['heading']    = 'Welcome to SMS';
$data['main_content']   = 'stud-prof';  // page name
return view('innerpages/template', $data);
}

public function UpStudProf(){
$crudModel = new Mod_Stud();
$s_id = $this->request->getPost('s_id');
$data = array(
    's_lrn'   => $this->request->getPost('s_lrn'),
    's_fname' => $this->request->getPost('s_fname'),
    's_mname' => $this->request->getPost('s_mname'),
    's_lname' => $this->request->getPost('s_lname'),
    );
$crudModel->upStud($data, $s_id);
return redirect()->to('Home/StudProfile'); //return to StudProfile
}

Routes.php

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

p4tfgftt1#

...是否可以删除URL上的控制器名称Home
仅使用定义的路线
当没有找到与URI匹配的已定义路由时,系统将尝试将该URI与上述控制器和方法进行匹配。您可以禁用此自动匹配,并将路由限制为仅由您定义的路由,方法是将setAutoRoute()选项设置为false

$routes->setAutoRoute(false);

其次,在禁用自动匹配后,声明您的用户定义路由:应用程序/配置/路由.php

$routes->get('student-profiles', 'Home::StudProfile');

最后:\应用程序\控制器\主页::UpStudProf
重定向(字符串$route)
参数:$route(string)-要将用户重定向到的反向路由或命名路由。
代替:

// ...
return redirect()->to('Home/StudProfile'); //return to StudProfile ❌
// ...

使用此选项:

// ...
return redirect()->to('/student-profiles'); ✅
// ...

相关问题