Codeigniter HMVC and CMS - codeigniter

I am using Codeigniter with the HMVC Modular extension and have a backend CMS area for managing website content. I am routing the base URL + "admin" (http://localhost/cms/admin) to controller methods with the prefix "admin_".
e.g. function admin_index() {...}
And here is my routing:
$route['admin/([a-zA-Z]+)/(:any)'] = "$1/admin_$2";
$route['^admin/(:any)(/)?'] = "$1/admin_index";
$route['^admin(/)?'] = "dashboard/admin_index";
$route['admin/logout'] = "login/admin_logout";
With the HMVC it is not routing correctly now. Here is what happens:
URL: http://localhost/cms/admin/faqs
Directory: modules/faqs/controllers/faqs - index method
--
here is where it breaks
--
URL: http://localhost/cms/admin/faqs/categories
Directory: modules/faqs/controllers/faqs - categories method (does not exits)
Desired: modules/faqs/controllers/categories - index method
How can I use HMVC while maintaining the "admin" are of the website?

You are making life a bit too tricky by putting frontend and backend functions in the same controllers. Have a look at my article on how to create an admin structure in CodeIgniter.

I'm working on something similar, and implemented a swapping like you did (3rd option) and it worked fine.
I tried to implement a front controller to handle the admin section, and run modules with HMVC modules::run() and buffer the output as I wish, but then I have faced another issue, you will have to change the URI schemes from / to _ or something else, since you wont be able to send module segments as parameter to your controller because CI relies on "/" for it's routing mechanism.
The only way is to emulate the admin section as Phil suggested, but there is another option to still have control over the code implemented by anyone using your CMS.
You could extend CI_Controller (or MX_Controller in case you are using HMVC) and add an Admin_Controller which will handle your logic and control what modules can do.
Have a look at this CodeIgniter Base Classes: Keeping it DRY

Related

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 and main controllers,models and views

I decided to try HMVC pattern with codeigniter but I have some doubts about how to think about and build my website structure using this pattern so I have some questions:
if the main focus is on modules what is the purpose of application/controllers, application/views and application/models.
can I remove the aboved folders and route the default controller to some module?
if I have 3 controller each of them haveing unique $type and $id but all of them need to call a controller that control every thing about comments in website and just pass $type and $id, will this confilct with HMVC pattern?
the purpose of this 3 folders is to have the most 'generic' things on your application. For example, if you have a crud model, you should have on the main model folder, outside your modules. Other example, if you have a generic header/footer view you should have it in the main views folder, and so on.
You shouldn't remove this folders, but you can set the default controller just adding the module in front.
I think this is not a problem, it will not be any conflict on hvmc pattern
here you have a good guide

Making codeigniter admin to rule them all

I want to make a codeigniter admin/cms to manage all my clients front ends, i want to separate the admin application in two folder, the generic folder, that all other will extend or use, and the client site admin, that i will make for the client needs.
Can it be done?
i recommend the WireDesigns HMVC - https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
This will allow you to make 2 modules Front-End / Admin in each module it follows hte same rules of Controller / Model / View which you can build up each one independently but to work together.
Technically though you could just create 2 folders in your controllers folder called Frontend / Admin, and build your controllers right there.
but if you looking for cleaner ogranization, try the HMVC

How do I change the index page in a CodeIgniter anchor?

So, I have two different applications in my CodeIgniter installation. One is admin, the other is frontend. I basically just copied the index file, renamed it "admin.php", and changed the application directory to "application/admin". I then changed the application directory in index.php to "application/frontend".
What I would like to do is create a link on the frontend application that takes you to the admin application. The variable config['index_page'] in the frontend application is set to "index.php" and in the admin application it's set to "admin.php".
Is there a way to set the url helper to use "admin.php" instead of "index.php"?
You don't need to do that way.
you should make or use an authentication library and you set different roles for different
users.
you just after login can put the redirection to your admin controller.
and for other users and viewers you can redirect them to any other controllers.
for viewers you can just use something like this:
Code:
if(!$this->m_auth->is_logged_in())
{
$this->viewers();
}
else
{
$this->users();
}
In your users function you just check different roles and redirect according.
I think you are missing some codeigniter concept, and you are trying to do it the normal way, i suggest you to read this article , you will how you can use MY_Controller as same concept of front controller and how you will be able to give every use specific roles
another way is to use a ready made authentication library as #medhi said
I would recommend Tank Authentication or Ion Auth
I

Zend multiple directories and MVC routing

I'm building my first personal website using Zend 1.11. To create the directory structure I've used the zf script that comes with the framework. I can use the same script to create controllers/views like so:
/home
/contact
/blog
/about
Yet where I've come unstuck is trying to encompass multiple directories e.g.
/contact/address
/about/cv/workhistory
...I just get an error saying the content cannot be found. There is no model, just a controller (route?) and a view. I've tried adding the desired directory structure when I use zf but this results in an error e.g.
zf create controller about/cv/workhistory
Is this even possible in MVC or does it show a lack of understanding of what MVC is on my part? Thanks in advance for any help/tips/pointers
ok you've created some controllers. Now it looks as though you want to add some functions to your controllers.
In order to do /contact/address it would be appropriate to have the addressAction() inside your ContactController().
A controller is a group of actions (read METHODS). When you built your application 2 controllers were built with it, one of them was the IndexController() that also gave you your first action indexAction() and your first view index.phtml located at /application/views/scripts/index:
<?php
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
}
}
Zend Framework implements the MVC paradigm with actual Models, Views and Controllers. In the controllers are your actions grab data and prepare it to be viewed.
Go ahead and do yourself a favor and run through some tutorials, here are some suggestions.
Rob Allens ZF 1.11 tutorial
Zend Framework Quickstart
The naming conventions are important to understand and can be found at:
Zend Framework Naming conventions

Resources