can't create instance of a model class in models from controller class |
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?
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 )
@InsiteFX cheers for the heads up
in my controller Home.php(in Controller dir ) i have PHP Code: <?php namespace App\Controllers; then in my model NewsModel.php (in Models dir) I have PHP Code: <?php namespace App\Models; 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; PHP Code: $model = new NewsModel();
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: #-------------------------------------------------------------- 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 )
(11-12-2019, 09:07 AM)captain-sensible Wrote: @InsiteFX cheers for the heads up 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; APPPATH/Models/newsModel.php PHP Code: <?php namespace App\Models; FWIW, I don't see any issue with your Apache site config. Have you made any changes to /Config/Autoload.php ?
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.
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 |
Welcome Guest, Not a member yet? Register Sign In |