Codeigniter 4 路线未按预期工作,视图混乱
Codeigniter 4 routes not working as expected, disorganizes views
我是 CI4 的新手,我已经为此奋斗了数周。我映射了我的路线,没有占位符它工作正常,但是一旦我添加占位符,它就会扰乱我的视图。
我尝试了一些知识的改变,但没有很好的结果
Routes.php
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Login');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);
$myroutes = [];
$myroutes['login'] = 'Login::index'; //working
$myroutes['logout'] = 'Login::logout'; //working
$myroutes['home'] = 'Home::dashboard'; //working
//User
$myroutes['add_user'] = 'User::create'; //working
$myroutes['view_users'] = 'User::user_list'; //working
//Client
$myroutes['addClient'] = 'Client::create'; //working
$myroutes['clients'] = 'Client::view'; //working
$myroutes['clientProfile/(:num)'] = 'Client::profile/'; //not working
$myroutes['rooms'] = 'Home::rooms'; //working
$myroutes['privileges/(:num)'] = 'Home::permissions/'; //not working
$routes->map($myroutes);
这是我的控制器脚本。除 profile 方法外,所有其他方法都工作正常
控制器(Client.php)
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\ClientModel;
class Client extends BaseController
{
public $session;
public $clientModel;
public function __construct()
{
helper('form');
$this->clientModel = new ClientModel();
$this->session = \Config\Services::session();
}
public function create()
{
if (!session()->has('logged_user')) {
return redirect()->to('/login');
}
$data = [
'page_name' => 'Add Client',
'page_title' => 'Add Client | The Ashokas',
];
$data['validation'] = null;
$rules = [
'client_title' => 'required|min_length[2]|max_length[5]',
'first_name' => 'required|min_length[3]|max_length[16]',
'middle_name' => 'required|min_length[3]|max_length[16]',
'last_name' => 'required|min_length[3]|max_length[16]',
'relationship_status' => 'required',
'client_email' => 'required|valid_email|is_unique[clients.client_email]',
'mobile' => 'required|numeric|exact_length[11]',
'gender' => 'required',
'occupation' => 'required',
'workplace' => 'required',
'address' => 'required',
'kin_name' => 'required|min_length[3]|max_length[50]',
'kin_relationship' => 'required',
'kin_mobile' => 'required|numeric|exact_length[11]',
'kin_address' => 'required'
];
if ($this->request->getMethod() == 'post') {
if ($this->validate($rules)) {
$clientdata = [
'client_title' => $this->request->getVar('client_title'),
'first_name' => $this->request->getVar('first_name'),
'middle_name' => $this->request->getVar('middle_name'),
'last_name' => $this->request->getVar('last_name'),
'relationship_status' => $this->request->getVar('relationship_status'),
'client_email' => $this->request->getVar('client_email'),
'mobile' => $this->request->getVar('mobile'),
'gender' => $this->request->getVar('gender'),
'occupation' => $this->request->getVar('occupation'),
'workplace' => $this->request->getVar('workplace'),
'address' => $this->request->getVar('address'),
'kin_name' => $this->request->getVar('kin_name'),
'kin_relationship' => $this->request->getVar('kin_relationship'),
'kin_mobile' => $this->request->getVar('kin_mobile'),
'kin_address' => $this->request->getVar('kin_address'),
'join_date' => date('d-m-Y')
];
if ($this->clientModel->create($clientdata)) {
$this->session->setTempdata('success', 'Client added successfully',3);
return redirect()->to('/clients');
}else{
$this->session->setTempdata('error', 'Sorry! Unable to add client',3);
return redirect()->to(current_url());
}
}else{
$data['validation'] = $this->validator;
}
}
return view('client/add', $data);
}
public function view() {
if (!session()->has('logged_user')) {
return redirect()->to('/login');
}
$data = [
'page_name' => 'Clients',
'page_title' => 'All Clients | The Ashokas',
'clientele' => $this->clientModel->getAllClients()
];
return view('client/view', $data);
}
public function profile($client_id = null) {
if (!session()->has('logged_user')) {
return redirect()->to('/login');
}
$data = [
'page_name' => 'Client Profile',
'page_title' => 'Client Profile | The Ashokas',
];
return view('client/profile', $data);
}
}
预期观看次数
我得到的观点
请各位高手赐教。感谢期待
您的 javascript 和 css 路径似乎是相对于 html header 中的当前文件夹的。当你在 /path1/path2
时,这个两级结构工作正常,但是当你有 /path1/path2/1
时,这是三级结构,它会因相关 js 和 css 文件而失败。
将它们更改为绝对路径。
你可能有这样的事情
<link rel="stylesheet" type="text/css" href="../../somefolder/mycss.css">
改成这样
<link rel="stylesheet" type="text/css" href="/appfolder/somefolder/mycss.css">
对 javascript 文件和图像也做同样的事情。
尝试添加 base_url 函数来创建这样的绝对路径
<link href="<?= base_url('path/of/your/css.css') ?>" rel="stylesheet">
请注意 path/of/your/css.css
存储在 public 文件夹中
我是 CI4 的新手,我已经为此奋斗了数周。我映射了我的路线,没有占位符它工作正常,但是一旦我添加占位符,它就会扰乱我的视图。
我尝试了一些知识的改变,但没有很好的结果
Routes.php
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Login');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);
$myroutes = [];
$myroutes['login'] = 'Login::index'; //working
$myroutes['logout'] = 'Login::logout'; //working
$myroutes['home'] = 'Home::dashboard'; //working
//User
$myroutes['add_user'] = 'User::create'; //working
$myroutes['view_users'] = 'User::user_list'; //working
//Client
$myroutes['addClient'] = 'Client::create'; //working
$myroutes['clients'] = 'Client::view'; //working
$myroutes['clientProfile/(:num)'] = 'Client::profile/'; //not working
$myroutes['rooms'] = 'Home::rooms'; //working
$myroutes['privileges/(:num)'] = 'Home::permissions/'; //not working
$routes->map($myroutes);
这是我的控制器脚本。除 profile 方法外,所有其他方法都工作正常 控制器(Client.php)
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\ClientModel;
class Client extends BaseController
{
public $session;
public $clientModel;
public function __construct()
{
helper('form');
$this->clientModel = new ClientModel();
$this->session = \Config\Services::session();
}
public function create()
{
if (!session()->has('logged_user')) {
return redirect()->to('/login');
}
$data = [
'page_name' => 'Add Client',
'page_title' => 'Add Client | The Ashokas',
];
$data['validation'] = null;
$rules = [
'client_title' => 'required|min_length[2]|max_length[5]',
'first_name' => 'required|min_length[3]|max_length[16]',
'middle_name' => 'required|min_length[3]|max_length[16]',
'last_name' => 'required|min_length[3]|max_length[16]',
'relationship_status' => 'required',
'client_email' => 'required|valid_email|is_unique[clients.client_email]',
'mobile' => 'required|numeric|exact_length[11]',
'gender' => 'required',
'occupation' => 'required',
'workplace' => 'required',
'address' => 'required',
'kin_name' => 'required|min_length[3]|max_length[50]',
'kin_relationship' => 'required',
'kin_mobile' => 'required|numeric|exact_length[11]',
'kin_address' => 'required'
];
if ($this->request->getMethod() == 'post') {
if ($this->validate($rules)) {
$clientdata = [
'client_title' => $this->request->getVar('client_title'),
'first_name' => $this->request->getVar('first_name'),
'middle_name' => $this->request->getVar('middle_name'),
'last_name' => $this->request->getVar('last_name'),
'relationship_status' => $this->request->getVar('relationship_status'),
'client_email' => $this->request->getVar('client_email'),
'mobile' => $this->request->getVar('mobile'),
'gender' => $this->request->getVar('gender'),
'occupation' => $this->request->getVar('occupation'),
'workplace' => $this->request->getVar('workplace'),
'address' => $this->request->getVar('address'),
'kin_name' => $this->request->getVar('kin_name'),
'kin_relationship' => $this->request->getVar('kin_relationship'),
'kin_mobile' => $this->request->getVar('kin_mobile'),
'kin_address' => $this->request->getVar('kin_address'),
'join_date' => date('d-m-Y')
];
if ($this->clientModel->create($clientdata)) {
$this->session->setTempdata('success', 'Client added successfully',3);
return redirect()->to('/clients');
}else{
$this->session->setTempdata('error', 'Sorry! Unable to add client',3);
return redirect()->to(current_url());
}
}else{
$data['validation'] = $this->validator;
}
}
return view('client/add', $data);
}
public function view() {
if (!session()->has('logged_user')) {
return redirect()->to('/login');
}
$data = [
'page_name' => 'Clients',
'page_title' => 'All Clients | The Ashokas',
'clientele' => $this->clientModel->getAllClients()
];
return view('client/view', $data);
}
public function profile($client_id = null) {
if (!session()->has('logged_user')) {
return redirect()->to('/login');
}
$data = [
'page_name' => 'Client Profile',
'page_title' => 'Client Profile | The Ashokas',
];
return view('client/profile', $data);
}
}
预期观看次数
我得到的观点
请各位高手赐教。感谢期待
您的 javascript 和 css 路径似乎是相对于 html header 中的当前文件夹的。当你在 /path1/path2
时,这个两级结构工作正常,但是当你有 /path1/path2/1
时,这是三级结构,它会因相关 js 和 css 文件而失败。
将它们更改为绝对路径。
你可能有这样的事情
<link rel="stylesheet" type="text/css" href="../../somefolder/mycss.css">
改成这样
<link rel="stylesheet" type="text/css" href="/appfolder/somefolder/mycss.css">
对 javascript 文件和图像也做同样的事情。
尝试添加 base_url 函数来创建这样的绝对路径
<link href="<?= base_url('path/of/your/css.css') ?>" rel="stylesheet">
请注意 path/of/your/css.css
存储在 public 文件夹中