猿问

codeigniter 4 - 找不到控制器或其方法

当我在本地服务器中测试时它运行良好,但是当我上传 000webhost 服务器时它显示错误。

错误 我的路线:

我的路线:

$routes->get('/komik/create', 'komik::create');

$routes->get('/komik/edit/(:segment)', 'komik::edit/$1');

$routes->delete('/komik/(:num)', 'komik::delete/$1');

$routes->get('/komik/(:any)', 'Komik::detail/$1');

有人可以帮助我吗?

一个错误

Controller or its method is not found: \App\Controllers\komik::create

我尝试在 url 中隐藏详细信息的方法,它在本地服务器中运行良好,但为什么当我上传到网络托管时它显示错误


喵喵时光机
浏览 140回答 2
2回答

一只甜甜圈

你好,我只是看看你的代码,我认为 codeigniter 4 在检测路由方面更强大。根据您的路线,您甚至不需要创建这些路线。调用您的控制器和方法而不创建路由。就这个namespace App\Controllers;use App\Models\Data_KomikModel;use App\Models\GambarModel;use App\Models\SoalModel;class Komik extends BaseController{&nbsp; &nbsp; protected $komikModel;&nbsp; &nbsp; public function __construct()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->komikModel = new Data_KomikModel();&nbsp; &nbsp; &nbsp; &nbsp; $this->gambarModel = new GambarModel();&nbsp; &nbsp; &nbsp; &nbsp; $this->soalModel = new SoalModel();&nbsp; &nbsp; &nbsp; &nbsp; // dd(session()->get('email'));&nbsp; &nbsp; }&nbsp; &nbsp; public function index()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (!session()->has('username')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return redirect()->to('/auth/login');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // $komik = $this->komikModel->findAll();&nbsp; &nbsp; &nbsp; &nbsp; $data = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'title' => 'Data Komik',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'komik' => $this->komikModel->getKomik()&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; // $komikModel = new \App\Models\Data_KomikModel();&nbsp; &nbsp; &nbsp; &nbsp; // $komikModel = new Data_KomikModel();&nbsp; &nbsp; &nbsp; &nbsp; return view('dataKomik/komik', $data);&nbsp; &nbsp; }&nbsp; &nbsp; public function detail($slug)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $komik = $this->komikModel->getKomik($slug);&nbsp; &nbsp; &nbsp; &nbsp; $gambar = $this->gambarModel->getGambar($slug);&nbsp; &nbsp; &nbsp; &nbsp; $soal = $this->soalModel->getDataSoal($slug);&nbsp; &nbsp; &nbsp; &nbsp; // dd($soal);&nbsp; &nbsp; &nbsp; &nbsp; $data = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'title' => 'Info Komik',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'komik' => $komik,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'gambar' => $gambar,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'soal' => $soal,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'validation' => \Config\Services::validation()&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; if (empty($data['komik'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new \CodeIgniter\Exceptions\PageNotFoundException('Judul Komik ' . $slug . 'Tidak Ditemukan');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return view('dataKomik/detail', $data);&nbsp; &nbsp; }&nbsp; &nbsp; public function create()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // session();&nbsp; &nbsp; &nbsp; &nbsp; $data = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'title' => 'Form Tambah Data Komik',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'validation' => \Config\Services::validation()&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; return view('dataKomik/create', $data);&nbsp; &nbsp; }&nbsp; &nbsp; public function save()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (!$this->validate([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'judul' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'rules' => 'required|is_unique[data_komik.judul]',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'errors' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'required' => '{field} komik harus diisi.',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'is_unique' => '{field} komik sudah terdaftar'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'sampul' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'rules' => 'max_size[sampul,1024]|is_image[sampul]|mime_in[sampul,image/jpg,image/jpeg,image/png]',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'errors' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'max_size' => 'ukuran gambar terlalu besar',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'is_image' => 'yang anda pilih bukan gambar',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'mime_in' => 'yang anda pilih bukan gambar',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; ])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // $validation = \Config\Services::validation();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // return redirect()->to('/komik/create')->withInput()->with('validation', $validation);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return redirect()->to('/komik/create')->withInput();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $fileSampul = $this->request->getFile('sampul');&nbsp; &nbsp; &nbsp; &nbsp; if ($fileSampul->getError() == 4) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $namaSampul = 'default.jpg';&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // $namaSampul = $fileSampul->getName();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $namaSampul = $fileSampul->getRandomName();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fileSampul->move('img', $namaSampul);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $slug = url_title($this->request->getVar('judul'), '-', true);&nbsp; &nbsp; &nbsp; &nbsp; $this->komikModel->save([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'judul' => $this->request->getVar('judul'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'slug' => $slug,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'sampul' => $namaSampul,&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; session()->setFlashdata('pesan', '<div class="alert alert-success alert-dismissible fade show" role="alert">&nbsp; &nbsp; &nbsp; &nbsp; Data Berhasil Ditambahkan.&nbsp; &nbsp; &nbsp; &nbsp; <button type="button" class="close" data-dismiss="alert" aria-label="Close">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span aria-hidden="true">&times;</span>&nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; </div>');&nbsp; &nbsp; &nbsp; &nbsp; return redirect()->to('/komik');&nbsp; &nbsp; }&nbsp; &nbsp; public function delete($id)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $komik = $this->komikModel->find($id);&nbsp; &nbsp; &nbsp; &nbsp; if ($komik['sampul'] != 'default.jpg') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unlink('img/' . $komik['sampul']);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $this->komikModel->delete($id);&nbsp; &nbsp; &nbsp; &nbsp; session()->setFlashdata('pesan', '<div class="alert alert-success alert-dismissible fade show" role="alert">&nbsp; &nbsp; &nbsp; &nbsp; Data Berhasil Dihapus.&nbsp; &nbsp; &nbsp; &nbsp; <button type="button" class="close" data-dismiss="alert" aria-label="Close">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span aria-hidden="true">&times;</span>&nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; </div>');&nbsp; &nbsp; &nbsp; &nbsp; return redirect()->to('/komik');&nbsp; &nbsp; }&nbsp; &nbsp; public function edit($slug)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $data = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'title' => 'Form Ubah Data Komik',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'validation' => \Config\Services::validation(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'komik' => $this->komikModel->getKomik($slug)&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; return view('dataKomik/edit', $data);&nbsp; &nbsp; }&nbsp; &nbsp; public function update($id)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $komikLama = $this->komikModel->getKomik($this->request->getVar('slug'));&nbsp; &nbsp; &nbsp; &nbsp; if ($komikLama['judul'] == $this->request->getVar('judul')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $rule_judul = 'required';&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $rule_judul = 'required|is_unique[data_komik.judul]';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (!$this->validate([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'judul' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'rules' => $rule_judul,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'errors' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'required' => '{field} komik harus diisi.',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'is_unique' => '{field} komik sudah terdaftar'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'sampul' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'rules' => 'max_size[sampul,1024]|is_image[sampul]|mime_in[sampul,image/jpg,image/jpeg,image/png]',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'errors' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'max_size' => 'ukuran gambar terlalu besar',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'is_image' => 'yang anda pilih bukan gambar',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'mime_in' => 'yang anda pilih bukan gambar',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; ])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return redirect()->to('/komik/edit/' . $this->request->getVar('slug'))->withInput();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $fileSampul = $this->request->getFile('sampul');&nbsp; &nbsp; &nbsp; &nbsp; $komik = $this->komikModel->find($id);&nbsp; &nbsp; &nbsp; &nbsp; if ($fileSampul->getError() == 4) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $namaSampul = $this->request->getVar('sampulLama');&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $namaSampul = $fileSampul->getRandomName();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fileSampul->move('img', $namaSampul);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($komik['sampul'] != 'default.jpg') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unlink('img/' . $this->request->getVar('sampulLama'));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $slug = url_title($this->request->getVar('judul'), '-', true);&nbsp; &nbsp; &nbsp; &nbsp; $this->komikModel->save([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id_komik' => $id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'judul' => $this->request->getVar('judul'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'slug' => $slug,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'sampul' => $namaSampul,&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; session()->setFlashdata('pesan', '<div class="alert alert-success alert-dismissible fade show" role="alert">&nbsp; &nbsp; &nbsp; &nbsp; Data Berhasil Diubah.&nbsp; &nbsp; &nbsp; &nbsp; <button type="button" class="close" data-dismiss="alert" aria-label="Close">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span aria-hidden="true">&times;</span>&nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; </div>');&nbsp; &nbsp; &nbsp; &nbsp; return redirect()->to('/komik');&nbsp; &nbsp; }&nbsp; &nbsp; //--------------------------------------------------------------------}然后在你的链接中使用这个<a href="<?= base_url('komik/create')?>"> Create</a>或者如果您仍然需要使用路由,请删除路由前面的正斜杠,而不是$routes->get('/komik/create', 'komik::create');$routes->get('/komik/edit/(:segment)', 'komik::edit/$1');$routes->delete('/komik/(:num)', 'komik::delete/$1');$routes->get('/komik/(:any)', 'Komik::detail/$1');用这个$routes->get('komik/create', 'komik::create');$routes->get('komik/edit/(:segment)', 'komik::edit/$1');$routes->delete('komik/(:num)', 'komik::delete/$1');$routes->get('komik/(:any)', 'Komik::detail/$1');我认为这有帮助!如果不引起我的注意

qq_遁去的一_1

注意区分大小写,更改:$routes->get('/komik/create',&nbsp;'komik::create');到 :$routes->get('/komik/create',&nbsp;'Komik::create');
随时随地看视频慕课网APP
我要回答