codeigniter structure for models views and controllers - codeigniter

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

Related

Handling multiple views controllers in CodeIgniter 4

I am new to CodeIgniter 4.
I have one controller named Pages and helps to display website pages normally. Then I have another controller named Admin which is suppose to load the admin view with its own header and footer files.
While Pages controller works fine, the Admin controller keeps saying 404-file not found admin.
I think I have a problem with my current Route settings below. Please help.
$routes->setDefaultController('Pages');
$routes->setDefaultMethod('default');
// Route Definitions
$routes->get('/', 'Pages::index');
$routes->get('(:any)', 'Pages::default/$1');
try ajusting your code like this.
$routes->setDefaultController('Pages');
$routes->setDefaultMethod('default');
// Route Definitions
$routes->resource('pages');
$routes->get('/', 'Pages::index');
$routes->get('(:any)', 'Pages::default/$1');

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 HMVC Need subfolders like superadmin admin employee

Codeigniter HMVC I need Folder like this, can anyone please help me?
Superadmin admin and employee have dashboard modules like as follows as More models with same user roles like SuperAdmin, Admin, and Employee.
Modules
Parent Folder - Dashboard
Superadmin
controllers models views
Admin
controllers models views
Employee
controllers models views`

Codeigniter Access model class outside root

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.

codeigniter: admin/cms as separate project or part of the same

So I've built a front-end to a website in Codeigniter, it has public and members areas.
Now it's time to build the backend administration where staff can manage some content: http://mysite.com/admin
At first I was going to just create subfolders within my controllers, models,views and resources folders for 'admin'
I'm wondering if I should just be doing a separate installation of CI for the admin? the layouts are different, the auth is different, sure they will share the same MySQL database but the admin will have different models and controllers
Best approach?
This article by Phil will give you a better idea:
http://philsturgeon.co.uk/blog/2009/07/Create-an-Admin-panel-with-CodeIgniter

Resources