CodeIgniter Forums
can't create instance of a model class in models from controller class - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: can't create instance of a model class in models from controller class (/showthread.php?tid=74814)



can't create instance of a model class in models from controller class - captain-sensible - 11-11-2019

Abstract of problem is this :

in app/controllers dir,  i have  files such as Home.php , News.php with class names matching

From  index method of Home class i can do this :

class Home extends BaseController
{
    public function index()
   
    {
       
   
    $handle= new News();
    $handle->index();


i.e I can create an instance of a class located in /app/Controllers from another class , the file  holding it also in controllers.

With a simple class in Models directory however i get : 

Class 'Andy\UserModel' not found

the Andy comes from me trying namespace global permutations.


the dir with codeigniter is called CI and is located at /var/www/htdocs apache on slackware

echo of APPPATH gives : /var/www/htdocs/CI/app/

i've checked permissions of files in /var/www/htdocs/CI/app/Models/ and they are at least read for all

set up on apache (i don't know if this has anything to do with it) is via virtual hosts eg

<VirtualHost 127.0.0.2:80>
  <Directory /var/www/htdocs/CI/public>
 Options Indexes   FollowSymLinks
Require all granted
AllowOverride All
</Directory>
ServerAdmin [email protected]
  ServerName CI.org
    DocumentRoot /var/www/htdocs/CI/public/
    ServerAlias www.CI.org
    #ErrorLog "/var/log/httpd/dummy-host.example.com-error_log"
    #C<ustomLog "/var/log/httpd/dummy-host.example.com-access_log" common
</VirtualHost>

using http://127.0.0.2/  home page works , a route a added works and from a controller i can access from a sqlite3 database. I was moving function of database data access from controller to model when found problem.

Any ideas?


RE: can't create instance of a model class in models from controller class - InsiteFX - 11-11-2019

With CodeIgniter 4 everything has a namespace so you need to tell it the namespace that
a class lives in etc;

PHP Code:
use App\Models\ModelName

or

PHP Code:
$newModel App\Models\ModelName(); 

Sounds like your not give it the correct namespace.


RE: can't create instance of a model class in models from controller class - captain-sensible - 11-12-2019

@InsiteFX cheers for the heads  up


in my controller Home.php(in Controller dir ) i have

PHP Code:
<?php namespace App\Controllers;
 use 
App\Models\NewsModel;
 

class 
Home extends BaseController
{
    public function index()
   
    
{
       
     $model 
= new NewsModel();  

    then in my model NewsModel.php (in Models dir) I have
PHP Code:
<?php namespace  App\Models


class 
NewsModel 


{
       public function index()
       
       
{
        
   
           
       



i don't want to extend any class , connect to a databse or anything else ,just get system to find class

Now if i understand Namespace & use correctly because i stated full namespace in controller

PHP Code:
use App\Models\NewsModel;

then i should be able to use short code 
PHP Code:
$model = new NewsModel();   

i just dont get it 
i wonder if its some sort of autoload issue or something else in system 



RE: can't create instance of a model class in models from controller class - InsiteFX - 11-12-2019

It maybe the way your Virtual Host is setup.

I' am on Windows 10 Pro x64 using XAMPP this is how I setup my vhosts for http: and https:

Code:
#--------------------------------------------------------------
# CI4 Development Tutorial News
#--------------------------------------------------------------

#--------------------------------------------------------------
# HTTP:
#--------------------------------------------------------------
<VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs/ci4tutorial/public_html"
    ServerName ci4tut.local
    ServerAlias ci4tut.local
    <Directory "C:/xampp/htdocs/ci4tutorial/public_html">
        Order allow,deny
        Allow from all
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

#--------------------------------------------------------------
# HTTPS:
#--------------------------------------------------------------
<VirtualHost *:443>
    DocumentRoot "C:/xampp/htdocs/ci4tutorial/public_html"
    ServerName ci4tut.local
    ServerAlias ci4tut.local
    SSLEngine on
    SSLCertificateFile "conf/ssl.crt/server.crt"
    SSLCertificateKeyFile "conf/ssl.key/server.key"
    <Directory "C:/xampp/htdocs/ci4tutorial/public_html">
        Options All
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

#--------------------------------------------------------------

You will notice that I use public_html not the standard public folder because that's the
way my live host is setup.


RE: can't create instance of a model class in models from controller class - dave friend - 11-12-2019

(11-12-2019, 09:07 AM)captain-sensible Wrote: @InsiteFX cheers for the heads  up


in my controller Home.php(in Controller dir ) i have

PHP Code:
<?php namespace App\Controllers;
 use 
App\Models\NewsModel;
 

class 
Home extends BaseController
{
    public function index()
   
    
{
       
     $model 
= new NewsModel();   

    then in my model NewsModel.php (in Models dir) I have
PHP Code:
<?php namespace  App\Models


class 
NewsModel 


{
       public function index()
       
       
{
 
   
    
    



i don't want to extend any class , connect to a databse or anything else ,just get system to find class

Now if i understand Namespace & use correctly because i stated full namespace in controller

PHP Code:
use App\Models\NewsModel;

then i should be able to use short code 
PHP Code:
$model = new NewsModel();   

i just dont get it 
i wonder if its some sort of autoload issue or something else in system 

I don't get it either because if I recreate what is quoted above it works perfectly.

For your reference here's what I did.

APPPATH/Controllers/Home.php

PHP Code:
<?php namespace App\Controllers;

use 
App\Models\NewsModel;

class 
Home extends BaseController
{
 public function 
index()
 {
 
$model = new NewsModel();
 echo 
$model->getSome('Hello World');
 }



APPPATH/Models/newsModel.php

PHP Code:
<?php namespace App\Models;

class 
NewsModel
{
 public function 
getSome($param)
 {
 return 
$param;
 }


FWIW, I don't see any issue with your Apache site config.

Have you made any changes to /Config/Autoload.php ?


RE: can't create instance of a model class in models from controller class - captain-sensible - 11-14-2019

thanks for the replies ; i am going to have a play with a couple new sub folders in /var/www/htdocs put php files in play with namespace & use of "use" and that it works and its not my Apache set up . i did edit php.ini /etc/httpd/extra/httpd-vhosts.conf
and /etc/httpd/httpd.conf i had one warning about curl and sqlite already being loaded.if if there is one area could be another, then i might go for fresh codeigniter install.


RE: can't create instance of a model class in models from controller class - captain-sensible - 12-09-2019

a model is just a class , after reading namespace primer i simply created another directory within app

i'm starting with keeping things simple ( i'm only playing with CI4 at this stage) using

<?php namespace App\Andy;
use CodeIgniter\Database\ConnectionInterface;

class DoStuff

{
protected $title;
protected $article;



public function index()
{
$db = \Config\Database::connect("default");

$query = $db->query("SELECT * FROM blog");
$row = $query->getRowArray();
return $row['title'].$row['article'];


}



i can retrieve from sqlite database ; i can also insert into database