-
upnormal
Newbie
-
Posts: 8
Threads: 1
Joined: Nov 2020
Reputation:
0
hello there,
im just learning codeigniter 4 from youtube "web programming unpas". so im followed his course to create form with post method to insert data.
after insert the data succesfully, it will redirect to the index file. then, after i tried to insert data, the data its inserted successfully, but cannot redirect and showing error
"localhost send an invalid response"
here the code
route.php
Code: $routes->get('/', 'pages::index');
$routes->get('/komik/create', 'komik::create');
$routes->get('/komik/(:segment)', 'komik::detail/$1');
controller/komik.php
Code: <?php
namespace App\Controllers;
use App\Models\komikmodel;
class komik extends BaseController
{
protected $komikmodel;
public function __construct()
{
$this->komikmodel = new komikmodel();
}
public function index()
{
$data = [
'title' => 'Daftar Komik' ,
'komik' => $this->komikmodel->getkomik()
];
return view('komik/index', $data);
}
public function detail($slug)
{
$data = [
'title' => 'Detail Komik',
'komik' => $this->komikmodel->getkomik($slug)
];
if(empty($data['komik']))
{
throw new \CodeIgniter\Exceptions\PageNotFoundException('Judul Komik '. $slug. 'Tidak Ditemukan');
}
return view('komik/detail', $data);
}
public function create()
{
$data = [
'title' => 'Form Tambah Data Komik'
];
return view('/komik/create', $data);
}
public function save()
{
$slug = url_title($this->request->getVar('judul'), '-', true);
$this->komikmodel->save([
'judul' => $this->request->getVar('judul'),
'slug' => $slug,
'penulis' => $this->request->getVar('penulis'),
'penerbit' => $this->request->getVar('penerbit'),
'sampul' => $this->request->getVar('sampul')
]);
session()->setFlashData('pesan', 'Data berhasil di tambahkan!');
return redirect()->to('/komik');
}
}
any suggestion will be appreciated
thanks
-
neoneeco
Junior Member
-
Posts: 28
Threads: 4
Joined: Nov 2020
Reputation:
0
Hi
where is the route /komik in your routes file ?
-
upnormal
Newbie
-
Posts: 8
Threads: 1
Joined: Nov 2020
Reputation:
0
(11-27-2020, 03:08 AM)neoneeco Wrote: Hi
where is the route /komik in your routes file ? you mean file location?
in app/Controllers/komik
-
neoneeco
Junior Member
-
Posts: 28
Threads: 4
Joined: Nov 2020
Reputation:
0
in the
routes.php :
PHP Code: $routes->get('/', 'pages::index');
$routes->get('/komik/create', 'komik::create'); $routes->get('/komik/(:segment)', 'komik::detail/$1');
add:
$routes->get('/komik/', 'komik::index');
this is the routes /komik in your routes file, the missing route (i think)
-
upnormal
Newbie
-
Posts: 8
Threads: 1
Joined: Nov 2020
Reputation:
0
(11-27-2020, 01:33 PM)neoneeco Wrote: in the
routes.php :
PHP Code: $routes->get('/', 'pages::index');
$routes->get('/komik/create', 'komik::create'); $routes->get('/komik/(:segment)', 'komik::detail/$1');
add:
$routes->get('/komik/', 'komik::index');
this is the routes /komik in your routes file, the missing route (i think)
still not working
-
InsiteFX
Super Moderator
-
Posts: 6,580
Threads: 331
Joined: Oct 2014
Reputation:
240
11-28-2020, 03:31 AM
(This post was last modified: 11-28-2020, 03:35 AM by InsiteFX.)
Take off all the beginning / slashes and it should work.
PHP Code: $routes->get('/', 'pages::index');
$routes->get('komik/', 'Komik::index'); $routes->get('komik/create', 'Komik::create'); $routes->get('komik/(:segment)', 'Komik::detail/$1');
Also your controller name should be capitalized, 'Komik::index' .
PHP Code: // class name must have the first letter capitalized class Komik extends BaseController
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
-
upnormal
Newbie
-
Posts: 8
Threads: 1
Joined: Nov 2020
Reputation:
0
(11-28-2020, 03:31 AM)InsiteFX Wrote: Take off all the beginning / slashes and it should work.
PHP Code: $routes->get('/', 'pages::index');
$routes->get('komik/', 'Komik::index'); $routes->get('komik/create', 'Komik::create'); $routes->get('komik/(:segment)', 'Komik::detail/$1');
Also your controller name should be capitalized,'Komik::index' .
PHP Code: // class name must have the first letter capitalized class Komik extends BaseController
still not working
-
neoneeco
Junior Member
-
Posts: 28
Threads: 4
Joined: Nov 2020
Reputation:
0
What if :
return redirect()->to(base_url());
return redirect()->to(base_url('komik/'));
try and see
did you try other things ?
|