• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tutorial (Class 'App\Models\NewsModel' not found)

#1
Good morning, is anyone already testing CI4 and managed to do the documentation tutorial?

I followed the tutorial at https://bcit-ci.github.io/CodeIgniter4/t...index.html

I'm having trouble trying to use the Model, I'm getting the error that the Model class could not be found.

I'm starting to test this new version of CI, as the way we loaded the Model changed, I wonder if anyone had the problem and solved it.

Thank you very much in advance

[Image: erro_ci4.jpg]

Controller

PHP Code:
<?php

use App\Models\NewsModel;

class 
News extends \CodeIgniter\Controller
{
    public function 
index()
    {
        
$model = new NewsModel(); // --> APPPATH/Controllers\News.php at line 9

        
$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');
    }


Model

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

    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');
    }

Reply

#2
You didn't namespace your model. Assuming your model is stored at applications/Models/NewsModel.php, you should have this line at the very top of the file:

Code:
namespace App\Models;

That's how the application knows where to find it.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply

#3
Thanks, I did not notice.

I would like to congratulate you for the excellent Framework.
Reply

#4
No problem. And it wasn't in there at the time. I've updated the tutorial though so others should be able to follow along with a little less problems.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply

#5
Hi
I just spent an hour before I found this post that helped me.

In the tutorial on:
https://codeigniter4.github.io/CodeIgnit...ction.html

It would be nice if

PHP Code:
namespace App\Models

is in the code for the model above:

PHP Code:
use CodeIgniter\Model


And CI4 looks very promising Smile
Kim
Reply

#6
Fix applied to development version, and is now reflected in the development user guide ... https://codeigniter4.github.io/CodeIgnit...ction.html
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.