0

My

Application/.htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Application/config/routes.php

$route['default_controller'] = "news";
$route['404_override'] = '';

Application/models/news_model.php

<?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();
    }
}
?>

Applications/controlers/news.php

<?php
class News extends CI_Controller {

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

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

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


    public function view($slug)
    {
        echo $slug;
        $data['news_item'] = $this->news_model->get_news($slug);
        var_dump($data);
        if (empty($data['news_item']))
        {
            show_404();
        }

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

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

Applications/views/index.php:

<?php foreach ($news as $news_item): ?>
    <?php var_dump($news_item); ?>
    <h2><?php echo "<pre>"; echo $news_item['title'] ?></h2>
    <div id="main">
        <?php echo $news_item['text'] ?>
    </div>
    <p><a href="news/<?php echo $news_item['slug'] ?>">View article</a></p>

<?php endforeach ?>

And Applications/views/view.php

<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];

The problem is that I can see index (wich lists my news) but when I click on a slug link it tries to go to:

/news/slug1

And it fires a not found error..

What am I missing here?

Toni Michel Caubet
  • 19,333
  • 56
  • 202
  • 378

3 Answers3

3

Link should be:

 <p><a href="/news/view/<?php echo $news_item['slug'] ?>">View article</a></p>
raidenace
  • 12,789
  • 1
  • 32
  • 35
  • The requested URL /demo/news/view/noticia2 was not found on this server. ( /demo/ is the project folder ) – Toni Michel Caubet May 07 '13 at 14:11
  • 1
    No! it should be `Link`. That's if you want to do it right. – Twisted1919 May 07 '13 at 14:11
  • 1
    @ToniMichelCaubet Your htaccess needs to stay in the demo folder, okay ? – Twisted1919 May 07 '13 at 14:12
  • @Twisted1919: When working with split structures in CI, it does not need to be so. But yeah site_url() is the default function to retrieve the site path, good point there. – raidenace May 07 '13 at 14:12
  • @Raidenace - yes it does, it matters when your project is not in the webroot. – Twisted1919 May 07 '13 at 14:13
  • @ToniMichelCaubet: Is your document root pointing to the server folder installation of CI? – raidenace May 07 '13 at 14:13
  • with '' the script crashes in the loop... – Toni Michel Caubet May 07 '13 at 14:14
  • @Twisted1919: No it does not - because it that were the case, three CI installations I have been maintaining for over four years now would be broken all over the place. And site_url just gets what is set in your config, so it has no internal resolution mechanism anyways – raidenace May 07 '13 at 14:15
  • @ToniMichelCaubet - have you seen my comment about where htaccess should go ? – Twisted1919 May 07 '13 at 14:15
  • @Raidenace - maybe you are right after all, i don't work with CI for over two years now, so i might be mistaken. – Twisted1919 May 07 '13 at 14:16
  • @Twisted1919: no problemo, in fact I had forgotten about site_url for a while, thanks for the reminder.. Lets see if we can help the OP get his stuff up! Tony what does echo site_url() give you? – raidenace May 07 '13 at 14:19
  • @ToniMichelCaubet - the htaccess looks okay, but it is not in the right place, move it in the demo folder, near index.php file – Twisted1919 May 07 '13 at 14:20
  • index.php is in /application/views/news/index.php shall i put it in news then? – Toni Michel Caubet May 07 '13 at 14:21
  • 1
    .htaccess should be in your root folder. It is the folder where the index.php file resides along with folders such as system and application – raidenace May 07 '13 at 14:22
  • 1
    Really? i just told you, put it in the demo folder, where your application folder is, on the same level as the main index.php file is. Makes sense ? – Twisted1919 May 07 '13 at 14:23
  • @Twisted1919: I got confused by what the OP said too... :| – raidenace May 07 '13 at 14:23
  • Ah and also, your app crashed on site_url() usage because site_url() if i remember correctly is loaded via the url helper. so you will need to load the url helper in your controller, or autoload it, in order to use it in your views. – Twisted1919 May 07 '13 at 14:26
  • Ok toni, this looks like something is messed up. lets backtrack a bit. First, type in your whole url as link and first ensure that works. Like `http://localhost/whatever_is_here/news/views/id`. Next check site_url in any case to see how much of the complete working url above does it resolve to. Then check which are the missing pieces. These information will be needed to reach a proper solution – raidenace May 07 '13 at 14:29
  • @ToniMichelCaubet - i posted an updated htaccess, please try that one – Twisted1919 May 07 '13 at 14:29
  • I thought that it might be more productive if i uploaded the project... http://www53.zippyshare.com/v/12049811/file.html – Toni Michel Caubet May 07 '13 at 14:29
  • Twisted1919 good point. Toni, you can add the helper into autoload config array if not already not there – raidenace May 07 '13 at 14:30
  • Like this? $autoload['helper'] = array('url'); (it didnt help... :() – Toni Michel Caubet May 07 '13 at 14:35
  • it works now!!!! I'm not really sure what was the problem because i think there might be a few... – Toni Michel Caubet May 07 '13 at 14:44
0

Controller's index method should be:

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

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

and article link must be:

<p><a href="news/index/<?php echo $news_item['slug'] ?>">View article</a></p>
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
kodmanyagha
  • 932
  • 12
  • 20
0

Assuming your CI is in the /demo folder:
try this htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /demo/

#Needed for CodeIgniter
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|pub|tmp|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

</IfModule>

Your action should be accessible at a url like:
http://localhost:8080/demo/news/view/slug-here
and NOT at:
http://localhost:8080/demo/news/slug-here

Twisted1919
  • 2,430
  • 1
  • 19
  • 30