• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can't create instance of a model class in models from controller class

#1
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 webmaster@CI.org
  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?
Reply

#2
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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
@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 
Reply

#4
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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#5
(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 ?
Reply

#6
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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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