Files
coolcoth.com/app/Controllers/Admin/CategoryController.php
T
2026-08-08 16:26:31 +08:00

68 lines
2.2 KiB
PHP

<?php
namespace App\Controllers\Admin;
use App\Models\Category;
class CategoryController extends AdminController
{
public function index()
{
return $this->view('admin/categories', [
'cats' => (new Category())->all(),
'seg' => $this->seg(),
]);
}
public function create()
{
return $this->view('admin/category_form', ['c' => null]);
}
public function store()
{
if (!csrf_check()) { $this->redirect('admin/categories'); }
$cat = new Category();
$id = $cat->insert([
'name' => $this->post('name'),
'slug' => '',
'icon' => $this->post('icon', '❄'),
'description' => $this->post('description'),
'sort_order' => (int)$this->post('sort_order', 0),
'status' => $this->post('status', 1) ? 1 : 0,
'layout' => $this->post('layout', ''),
]);
// URL 标识留空时按记录序号顺序生成(短、稳定)
$slug = $this->post('slug') ? slugify($this->post('slug')) : (string)$id;
$cat->update($id, ['slug' => $slug]);
$this->redirect('admin/categories');
}
public function edit($id)
{
$c = (new Category())->find($id);
if (!$c) { $this->redirect('admin/categories'); }
return $this->view('admin/category_form', ['c' => $c]);
}
public function update($id)
{
if (!csrf_check()) { $this->redirect('admin/categories'); }
(new Category())->update($id, [
'name' => $this->post('name'),
'slug' => $this->post('slug') ? slugify($this->post('slug')) : (string)$id,
'icon' => $this->post('icon', '❄'),
'description' => $this->post('description'),
'sort_order' => (int)$this->post('sort_order', 0),
'status' => $this->post('status', 1) ? 1 : 0,
'layout' => $this->post('layout', ''),
]);
$this->redirect('admin/categories');
}
public function delete($id)
{
if (csrf_check()) { (new Category())->delete($id); }
$this->redirect('admin/categories');
}
}