Codeigniter Access model class outside root - codeigniter

I want load a model in root directory and access the model function in that file.
For example : I'm having test.php in root directory and having model class user.php in model folder.
In that user.php i have written the function named as fetch_all_user() it will return all the user of my database.
Now I want to access the fetch_all_user() function in test.php which is placed in root directory.
Please help

I don't know why would you do that but here it is
require(APPPATH.'user.php');
Put above line in test.php and only if your user.php is in root of your app. But you have to know that's not the best practice for codeigniter or any MVC framework. I would do just as complex857 said.

Related

How to create a subfolder inside controller and view folders in codeigniter?

I wanted to create a folder named 'schoollevel' inside controller and view folder to create a separate module containing login function and profile views to provide another login functionality for some users. The 'schoollevel' contains all the files for login and profile view. Can anyone suggest how to do this ? I want the control to go to the separate folder.
Its Easy to create folder in controller and view in codeigniter.
Folder name : school
1.create a login controller
application->controller->school->Login.php
create a login view
application->view->school->login.php
Now, How to call view file
Inside login controller in the index() function add
this to call login.php view
$this->load->view('school/login');
I hope this help you!

How to call two controllers in one controller folder when using hmvc CodeIgniter?

I have the following folder structure
Modules
--controllers
--User
--Product
How can I call the product controller from user controller from one particular function?
When I run this url http://[::1]/stagingweb/index.php/user/product I get the error 'The page you requested was not found'.
it looks like you have a problem to understand the concept of hmvc here
HMVC stands for Hierarchical model–view–controller, which means in Wiredesignz HMVC there is an additional variation called modules added to the classical MVC pattern used by Codeigniter.
in your case if you have users and products, its probably the best to create 2 modules (users and products).
So your folder structure would look like
modules
- users
- controllers
User.php
- models
- views
- products
- controllers
Product.php
- models
- views
in Wiredesignz HMVC Integration there is a class MX_Controller, so every module controller has to extend from it.
an example
class Product extends MX_Controller{}
And if you want to call another modules controller within your specific controller you simply have to call
$return = modules::run('products/product/your_function');
Though in most cases it's probably a cleaner solution to just call the models from the other modules instead of executing a controllers function...
The entire process is very well documented here

CodeIgniter url not working for new class

I uploaded CodeIgniter in my server, and the basic page works fine.
Then I added home.php file in application/controllers folder with class Hello and function one, and tried to open the link url/index.php/Hello/one but I get the codeIgniter error that the page isn't found. What can I be missing?
To fix your issue you should rename the Hello class to Home then go to URL/index.php/home/one
I recommend that you read up on the controller documentation:
http://ellislab.com/codeigniter/user-guide/general/controllers.html
A Controller is simply a class file that is named in a way that can be
associated with a URI.
Consider this URI: example.com/index.php/blog/
In the above example, CodeIgniter would attempt to find a controller
named blog.php and load it.
When a controller's name matches the first segment of a URI, it will
be loaded.

codeigniter structure for models views and controllers

I'm new to codeigniter and I'm creating a blog coded with it for learning, but i have some doubts about the structure.
Right now i have an admin section for login which is working and posts which work too, but i think it can be done better.
The project is in github
Now it is like this:
+Controllers
+admin
- users.php (login, logout)
+posts
- posts.php (index, new_post, delete_post, edit_post)
+Models
+admin
- user.php
+posts
- post.php
+Views
+admin
- login.php
+posts
- post.php
- post_index.php
- edit_post.php
- new_post.php
+components
- header.php
- footer.php
- mainsidebar.php
Is this structure ok or is there a better way of organizing/renaming it?
For the admin area i want to admin posts, should i move the posts views, controller and model to admin folder? or should i have posts in both admin and posts folders?
I'm a little bit lost about where to put the controllers/models/views.
Should i have to edit the routes?
Here my suggestion structure
application
controllers
admin
users.php
posts.php
models
admin
user.php
post.php
views
admin
user.php
post.php

CodeIgniter controller in subfolder

I have a controller in a subfolder. CodeIgniter is giving a 404 page not found.
The controller works fine in the root controller folder. The controller also works fine in the 1st level subfolder. The controller breaks in the 2nd level subfolder.
Why would CodeIgniter not want you to user multiple subfolders?
Example:
Works: controllers/pages/HomeController.php
Broken: controllers/pages/users/HomeController.php
My Routes are like this:
Works: $route['default_controller'] = "pages/HomeController";
Broken: $route['default_controller'] = "pages/users/HomeController";
I wrote about this before, you just need to read the CI manual, but here is a quick blog entry I did which should get you back on track:
http://blog.biernacki.ca/2011/12/codeigniter-uri-routing-issue-with-controller-folders/
Example:
$route['account/manage/(:num)/(:any)'] = "account/manage/index/$1/$2";
CodeIgniter does not inherently allow for multiple controller folders. It may or may not work, but it is an undocumented quirk. Using the routes.php file you can virtualize any folder or controller structure you want, just take care to map your routes back to a controller and method in the Controllers folder.

Resources