Welcome Guest, Not a member yet? Register   Sign In
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


Messages In This Thread
Tutorial (Class 'App\Models\NewsModel' not found) - by Marcos Queiroz - 10-16-2017, 10:06 AM



Theme © iAndrew 2016 - Forum software by © MyBB