CodeIgniter 4:如何启用用户定义的路由?
CodeIgniter 4: How do you enable user defined routes?
我只是想知道如何在使用 UpStudProf
函数后重定向到 StudProfile
。在 运行 UpStudProf
函数之后,URL 变成了 http://localhost/csms/public/index.php/Home/StudProfile
,但它应该是 http://localhost/Home/StudProfile
并且是否可以删除控制器名称 Home
URL?
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);
... is it possible to remove the Controllers name Home
on the URL?
When no defined route is found that matches the URI, the system will
attempt to match that URI against the controllers and methods as
described above. You can disable this automatic matching, and restrict
routes to only those defined by you, by setting the setAutoRoute()
option to false
:
$routes->setAutoRoute(false);
其次,在禁用自动匹配后,声明您的 user-defined 路由:
app/Config/Routes.php
$routes->get('student-profiles', 'Home::StudProfile');
最后:\App\Controllers\Home::UpStudProf,
Parameters: $route (string) – The reverse-routed or named route to
redirect the user to.
而不是:
// ...
return redirect()->to('Home/StudProfile'); //return to StudProfile ❌
// ...
使用这个:
// ...
return redirect()->to('/student-profiles'); ✅
// ...
我只是想知道如何在使用 UpStudProf
函数后重定向到 StudProfile
。在 运行 UpStudProf
函数之后,URL 变成了 http://localhost/csms/public/index.php/Home/StudProfile
,但它应该是 http://localhost/Home/StudProfile
并且是否可以删除控制器名称 Home
URL?
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);
... is it possible to remove the Controllers name
Home
on the URL?
When no defined route is found that matches the URI, the system will attempt to match that URI against the controllers and methods as described above. You can disable this automatic matching, and restrict routes to only those defined by you, by setting the
setAutoRoute()
option tofalse
:
$routes->setAutoRoute(false);
其次,在禁用自动匹配后,声明您的 user-defined 路由: app/Config/Routes.php
$routes->get('student-profiles', 'Home::StudProfile');
最后:\App\Controllers\Home::UpStudProf,
Parameters: $route (string) – The reverse-routed or named route to redirect the user to.
而不是:
// ...
return redirect()->to('Home/StudProfile'); //return to StudProfile ❌
// ...
使用这个:
// ...
return redirect()->to('/student-profiles'); ✅
// ...