Quantcast
Channel: CodeIgniter Forums - All Forums
Viewing all articles
Browse latest Browse all 14348

Error 404

$
0
0
Hi,

I try codeigniter 4 offline with XAMPP. The tutorial with static page is ok. I see home.php and about.php correctly.

The Tutorial "News section" display error 404 when I go http://localhost/mysite/public/index.php/news

My code

public $baseURL = 'http://localhost/mysite/public/';
public $indexPage = 'index.php';

Controllers/News.php

PHP Code:
<?php

use App\Models\NewsModel;

class 
News extends \CodeIgniter\Controller{
 
   public function index(){
 
       $model = new NewsModel();

 
       $data = [
 
           'news'  => $model->getNews(),
 
           'title' => 'News archive',
 
       ];

 
       echo view('Templates/Header'$data);
 
       echo view('News/Index'$data);
 
       echo view('Templates/Footer');
 
   }

 
   public function view($slug null){
 
       $model = new NewsModel();

 
       $data['news'] = $model->getNews($slug);

 
       if (empty($data['news'])){
 
           throw new \CodeIgniter\PageNotFoundException('Cannot find the page: '$slug);
 
       }

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

 
       echo view('Templates/Header'$data);
 
       echo view('News/View'$data);
 
       echo view('Templates/Footer');
 
   }


Models/NewsModel.php

PHP Code:
<?php
class NewsModel extends \CodeIgniter\Model{
 
   protected $table 'news';

 
   public function getNews($slug false){
 
       if ($slug === false){
 
           return $this->findAll();
 
       }

     
   return $this->asArray()
     
   ->where(['slug' => $slug])
     
   ->first();
    }


.htacess

Code:
<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Routes
PHP Code:
$routes->add('(:any)''Pages::view/$1'); // Routeur des pages statiques
$routes->add('/''Home::index');

// Routes Module News
$routes->get('news/(:segment)''News::view/$1');
$routes->get('news''News::index');
$routes->add('(:any)''Pages::view/$1'); 

Views/News/Index.php

PHP Code:
<h2><?= $title ?></h2>

<?php if(!empty($news) && is_array($news)) : ?>

    <?php foreach ($news as $news_item): ?>

        <h3><?= $news_item['title'?></h3>

        <div class="main">
            <?= $news_item['text'?>
        </div>
        
        <p><a href="<?= '/news/'.$news_item['slug'?>">View article</a></p>

    <?php endforeach; ?>

<?php else : ?>

    <h3>No News</h3>

    <p>Unable to find any news for you.</p>

<?php endif ?>

Views/News/View.php

PHP Code:
<?php
echo '<h2>'.$news['title'].'</h2>';
echo 
$news['text']; 

Viewing all articles
Browse latest Browse all 14348

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>