0

I have a problem with codeigniter 3

in my file routes.php I have

$route['article/(:num)/(:any)'] = 'article/index/$1/$2';

in my file article.php I have

<?php

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

class Article extends Frontend_Controller {

public function __construct()
{
    parent::__construct();
    $this->data['recent_news'] = $this->article_m->get_recent();
}

public function index($id, $slug)
{
    // Fetch the article
    $this->article_m->set_published();
    $this->data['article'] = $this->article_m->get($id);

    // Return 404 if not found uri_string() return blog/comments/123
    count($this->data['article']) || show_404(uri_string());

    // Redirect if slug was incorrect
    $request_slug = $this->uri->segment(3);
    $set_slug = $this->data['article']->slug;
    if ($request_slug != $set_slug) {
        // with 301 redirect
        redirect('article/' . $this->data['article']->id . '/' . $this->data['article']->slug, 'location', 301);
    }
    // Load view
    add_meta_title($this->data['article']->title);
    $this->data['subview'] = 'article';
    $this->load->view('_main_layout', $this->data);
}

}

when I enter to the url http://ci-cms.com/article/6/confesion it's ok, but if I enter to the url http://ci-cms.com/article I have a problem as this:

An uncaught Exception was encountered

Type: ArgumentCountError

Message: Too few arguments to function Article::index(), 0 passed in C:\xampp\htdocs\ci-cms\system\core\CodeIgniter.php on line 532 and exactly 2 expected

Filename: C:\xampp\htdocs\ci-cms\application\controllers\article.php

Line Number: 12

Backtrace:

File: C:\xampp\htdocs\ci-cms\index.php Line: 320 Function: require_once

How can I resolve this problem?, please help me I'm new in this framework...

thanks.

1 Answer 1

0

What is it you are expecting to happen? Going to http://ci-cms.com/article should go to the index method. The error is stating that you are missing the $id and $slug parameters.

You could do something like this:

public function index($id = NULL, $slug = NULL)
{ 

But you would then need to add code to check to make sure that $id and $slug are not null.

Like this:

public function index($id = NULL, $slug = NULL)
{
    if (!is_null($id) && !is_null($slug))
    {
        // Fetch the article
        $this->article_m->set_published();
        $this->data['article'] = $this->article_m->get($id);

        // Return 404 if not found uri_string() return blog/comments/123
        count($this->data['article']) || show_404(uri_string());

        // Redirect if slug was incorrect
        $request_slug = $this->uri->segment(3);
        $set_slug = $this->data['article']->slug;
        if ($request_slug != $set_slug)
        {
            // with 301 redirect
            redirect('article/' . $this->data['article']->id . '/' . $this->data['article']->slug, 'location', 301);
        }
        // Load view
        add_meta_title($this->data['article']->title);
        $this->data['subview'] = 'article';
        $this->load->view('_main_layout', $this->data);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

If this answered your question please mark this as answered. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.