I'm trying to make a basic CRUD system.
I have a PostsController class:
Code:
<?php namespace App\Controllers;
use App\Models\Post;
class PostsController extends BaseController
{
public function index()
{
$post = new Post();
echo view('templates/header');
echo view('posts/index', ['posts' => $post->findAll()]);
echo view('templates/footer');
}
public function show($id)
{
$post = new Post();
$result = $post->find($id);
if( is_null($result) ) {
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
echo view('templates/header');
echo view('posts/show', ['post' => $result ]);
echo view('templates/footer');
}
public function create()
{
$postModel = new Post();
$rules = $postModel->validationRules;
if($this->request->getMethod() == 'post' && $this->validate($rules)) {
$postModel->save([
'title' => $this->request->getPost('title'),
'content' => $this->request->getPost('content')
]);
} else {
echo view('templates/header', ['title' => 'Create a new post']);
echo view('posts/create-form');
echo view('templates/footer');
}
}
public function edit($id)
{
$post = new Post();
$result = $post->find($id);
if( is_null($result) ) {
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
echo view('templates/header');
echo view('posts/edit-form', ['post' => $result ]);
echo view('templates/footer');
}
public function store()
{
$postModel = new Post();
$rules = $postModel->validationRules;
if($this->request->getMethod() == 'post' && $this->validate($rules)) {
$postModel->save([
'title' => $this->request->getPost('title'),
'content' => $this->request->getPost('content')
]);
} else {
echo view('templates/header');
echo view('posts/edit-form', ['validator' => $this->validator ]);
echo view('templates/footer');
}
//return redirect('create-post');
}
public function update($id)
{
$postModel = new Post();
$rules = $postModel->validationRules;
$model = new Post();
$post = $postModel->find($id);
if( is_null($post) ) {
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
if($this->request->getMethod() == 'post' && $this->validate($rules)) {
$postModel->update($id, [
'title' => $this->request->getPost('title'),
'content' => $this->request->getPost('content')
]);
return redirect('home');
} else {
$this->edit($id);
}
}
}
and my Post model:
Code:
<?php namespace App\Models;
use CodeIgniter\Model;
class Post extends Model
{
protected $table = 'posts';
protected $primaryKey = 'id';
protected $returnType = 'object';
protected $allowedFields = ['title', 'content'];
protected $validationRules = [
'title' => 'required|alpha_numeric_space|min_length[3]',
'content' => 'required|min_length[10]',
];
protected $validationMessages = [
'title' => [
'required' => 'Sorry, this field must not be empty.',
]
];
}
The main issue I have is that the custom error messages are not displayed when there is an error. It seems like the defaults. How do I get CI to use them?
Also, in the PostsController class I use this a lot:
PHP Code:
$rules = $postModel->validationRules;
if($this->request->getMethod() == 'post' && $this->validate($rules)) {
Is there a way for CI to just know that the rules have been defined and to use them rather than calling $this->validate() and having to inject the rules anyway. Why define them in the model in the first place if that is the case.
Additionally, there is a lot of code repetition in the PostsController class. Is that the CI way, or is there a way better way to keep things DRY?
Finally, is the main dev team happy to accept pull requests? I would really like to contribute to the project and wondered if that is possible? I have the same question for CI3. Are you looking for maintainers for that codebase? I could make another post for that though.