Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter 4th level route not working

routes.php


    $route['admin/news'] = 'admin_news/index'; //working
    $route['admin/news/(:any)'] = 'admin_news/view/$1'; //working
    $route['admin/news/create'] = 'admin_news/create'; //working
    $route['admin/news/edit/(:any)'] = 'admin_news/edit/$1'; //not-working
    $route['admin/news/delete/(:any)'] = 'admin_news/delete/$1'; //not-working

controllers: admin_news.php


    if (!defined('BASEPATH'))
        exit('No direct script access allowed');

    class Admin_news extends CI_Controller {

    public function __construct()
            {
                    parent::__construct();
                    $this->load->model('news_model');
                    $this->load->helper('url');

                    if(!$this->session->userdata('is_logged_in')){
                redirect('admin/login');
            }
            }

    public function index()
    {
            $data['news'] = $this->news_model->get_news();
            $data['title'] = 'News archive';

            $this->load->view('admin/includes/header', $data);
            $this->load->view('admin/news/index', $data);
            $this->load->view('admin/includes/footer');
    }

    public function view($slug = NULL)
    {
            $data['news_item'] = $this->news_model->get_news($slug);

            if (empty($data['news_item']))
            {
                    show_404();
            }

            $data['title'] = $data['news_item']['title'];

            //$this->load->view('templates/header', $data);
            $this->load->view('admin/news/view', $data);
            //$this->load->view('templates/footer');
    }

    public function create()
    {
        $this->load->helper('form');
        $this->load->library('form_validation');

        $data['title'] = 'Create a news item';

        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('text', 'Text', 'required');

        if ($this->form_validation->run() === FALSE)
        {
            $this->load->view('admin/includes/header', $data);
            $this->load->view('admin/news/create', $data);
            $this->load->view('admin/includes/footer');

        }
        else
        {
            $this->news_model->set_news();
            $this->load->helper('url');
            $this->index();
        }
    }



    public function edit($slug)
    {
        $data['news_item'] = $this->news_model->get_news($slug);

        if (empty($data['news_item']))
        {
            show_404();
        }

        $data['title'] = 'Edit: '.$data['news_item']['title'];

        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('title', 'title', 'required');
        $this->form_validation->set_rules('text', 'text', 'required');

        if($this->form_validation->run() === FALSE)
        {
            $this->load->view('admin/includes/header', $data);  
            $this->load->view('admin/news/edit', $data);
            $this->load->view('admin/includes/footer');
        }
        else
        {
            $this->news_model->update_news( $this->input->post('id'),
                                            $this->input->post('title'),
                                            $this->input->post('text'));


            $data['news_item'] = $this->news_model->get_news($slug);
            $this->load->view('admin/includes/header', $data);                                    
            $this->load->view('admin/news/success');
            $this->load->view('admin/news/edit', $data);
            $this->load->view('admin/includes/footer');   
        }
    }


    public function delete($id = NULL) {
            $this->news_model->delete_news($id);
            $this->load->helper('url');
            $this->index();
        }

    }

models: News_model.php


    class News_model extends CI_Model {

      public function __construct()
      {
        $this->load->database();
      }

      public function get_news($slug = FALSE)
      {
        if ($slug === FALSE)
        {
        $query = $this->db->get('news');
        return $query->result_array();
        }

        $query = $this->db->get_where('news', array('slug' => $slug));
        return $query->row_array();
      }

        public function set_news()
      {
        $this->load->helper('url');

        $slug = url_title($this->input->post('title'), 'dash', TRUE);

        $data = array(
        'title' => $this->input->post('title'),
        'slug' => $slug,
        'text' => $this->input->post('text')
        );

        return $this->db->insert('news', $data);
      }

     /*public function update_news($slug = FALSE) 
    {
        $this->load->helper('url');

        $slug = url_title($this->input->post('title'),'dash',TRUE);

        $data = array(
            'slug'  =>  $slug,
            'title' =>  $this->input->post('title'),
            'text'  =>  $this->input->post('text')
        );
        $this->db->where('slug', $slug);
        return $this->db->update('news', $data);
    }*/


    public function update_news($id, $title, $text) {
        $data = array(
            'title' => $title,
            'text' => $text
        );

        $this->db->where('id', $id);
        $this->db->update('news', $data);
    }

      public function delete_news($id = FALSE) 
        {
            $this->db->delete('news', array('id' => $id));
        }
    }

views: admin/news/edit.php


    <h2>Edit a news item</h2>
    <?php echo validation_errors(); ?>

    <?php echo form_open('news/edit/'.$news_item['slug']) ?>

    

Title <?php echo form_input('title',$news_item['title']); ?>

Text <?php echo form_textarea('text',$news_item['text']); ?>

<?php echo form_hidden('slug',$news_item['slug']); ?> <?php echo form_hidden('id',$news_item['id']); ?>

<?php echo form_submit('submit', 'Save Changes'); ?>

<?php echo form_close(); ?>
When I am access
`http://localhost/ciadmin/admin/news/edit/news-slug`
showing 404 Page Not Found message!!
like image 695
Sanu Avatar asked Jan 17 '16 20:01

Sanu


2 Answers

Put it this way:

$route['admin/news/delete/(:any)'] = 'admin_news/delete/$1';
$route['admin/news/edit/(:any)'] = 'admin_news/edit/$1';
$route['admin/news/create'] = 'admin_news/create';
$route['admin/news/(:any)'] = 'admin_news/view/$1';
$route['admin/news'] = 'admin_news/index';

Remember:

Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.

Docs.

like image 105
Tpojka Avatar answered Oct 26 '22 20:10

Tpojka


I also had some problem with 4.0.4 and I was confused with that.
In ci4 just defined

myController extends BaseController

Then in routes.php

$routes->get('/', 'myController::index');

See more https://codeigniter4.github.io/userguide/incoming/routing.html

like image 32
Khamad Ali Avatar answered Oct 26 '22 21:10

Khamad Ali