I am working quite closely with Codeigniter the PHP framework:
http://www.codeigniter.com/
Now I've added this Modular Extensions - HMVC to my Codeigniter framework.
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/overview
Now, I've created my own module in the modules folder and set up directories for controllers, models and views as instructed. However I'm struggling specifically with custom routing.
I have created the config directory in my module blog directory and I have created the routes.php file within.
Now to access my module in the browser i would go to localhost:8888/blog/ now I'm mostly asking out of curiosity, I wanted to create a custom route so that maybe I could access the page like localhost:8888/posts/ so I thought that setting up the following route would work:
$route['posts'] = 'blog';
or if I had a method called listings I could use
$route['posts/listings'] = 'blog/listings';
However this returns a 404 Page Not Found.
Is it possible to create custom routes like this in a module?
Setting up custom routes for HMVC Easy here are some examples below. You can use same techneique for CI3 Make sure you choose right version from here https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/downloads go to branches and choose your version The Default is for CI-2
$route['default_controller'] = 'catalog/common/welcome/index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
// Common
$route['admin'] = "admin/common/login/index";
$route['admin/dashboard'] = "admin/common/dashboard/index";
$route['admin/logout'] = "admin/common/logout/index";
$route['admin/register'] = "admin/common/register/index";
// Users
$route['admin/users/user_edit/(:any)'] = "admin/user/users/user_edit/$1";
$route['admin/users/user_password/(:any)'] = "admin/user/users/user_password/$1";
$route['admin/users/user_key/(:any)'] = "admin/user/users/user_key/$1";
For Example:
admin will be module name.
application modules / admin <-- Admin Module Name
application / modules / admin / controllers / common <-- Sub folder
application / modules / admin / controllers / users <-- Sub folder
Please watch this great tutorial for beginners on HMVC https://www.youtube.com/watch?v=8fy8E_C5_qQ
You can also download Htaccess from here http://www.insiderclub.org/downloads you may need to join for free to download David's Insider Club suitable for codeigniter.
Related
I am trying to intregrate HMVC to codeigniter. I have installed the MX files to thrid_party and uploaded the MY_Loader , MY_Loader and MY_Model to the application/core folder. it is working fine
I have two issues
1) How to add the module routes that overide the application routes
I am accessing module through the link localhost/domain/admin/portfolio
I have tried adding routes.php to the modules config application/modules/portfolio/config/routes.php with below details
$route['admin/portfolio'] = 'portfolio/admin/portfolio';
$route['admin/portfolio/add'] = 'portfolio/admin/portfolio/edit';
$route['admin/portfolio/edit/(:num)'] = 'portfolio/admin/portfolio/edit/$1';
On my root application config already added a routes
$route['admin'] = 'admin/login';
Because of this route 'admin/login' in the application/config/routes.php it is showing page not found. To fix this I have currently added the module/portfolio/config/routes`` above the 'admin/login'. Is there any other method instead of adding it to theapplication/config/routes`.
2) How to access the module view files
I have controller accessing the view files from application/controlles/admin/
$this->load->view('admin/view_header',$data);
$this->load->view('admin/view_portfolio',$data);
$this->load->view('admin/view_footer');
You have placed your Portfolio Controller under
application/modules/portfolio/controllers/admin
which is fine.
Your route (which will hit the index by default) should be
$route['admin/portfolio'] = 'portfolio/admin/portfolio';
Aside: other naming considerations
What I tend to do is to create a controller with admin in the name...
So I would have PortfolioAdmin.php or something along those lines, so I know by the file name, it's admin "Stuff", when I am playing with it in my Editor/IDE.
UPDATE:
In regards to your
Nor this works Modules::run('admin/portfolio', $data);
So you would then use the full controller name... Do not use routes, they are for URLs. Any module you want to call from another module you always use the full name.
Modules::run('portfolio/admin/portfolio', $data);
I have a project that contains several subfolders, such as:
client/auth/login = model/view/controller
client/auth/signup = model/view/controller
admin/signin = model/view/controller
I set up my routes like this:
$route['default_controller'] = "admin/signin/signin";
$route['admin/sigin'] = "admin/sigin/signin/index";
$route['admin/(:any)'] = "admin/sigin/signin/";
$route['client/auth'] = "auth/login/login";
These routes are not working, which shows CodeIgniter 404 error page.
$route['default_controller'] = "admin/signin/signin";
$route['admin/signin'] = "admin/signin/signin/index";
$route['admin/(:any)'] = "admin/signin/signin/$1";
$route['client/auth'] = "auth/login/login";
Fixed typo's above.
And I think your file structure is not correct. I use CI2, not sure how modules work in CI3. But the modules 'forgot_password' and 'signin' would be using the same models right? Why having them in separate folders/modules? This way when you make a change to the User model, you'll have to make the change in every User model in all modules (unless you don't need it in that cases, but still I wouldn't risk building my app like that)
- modules
- Admin
- controllers
- user.php // Will have methods like signin(), add(), view(),...
- Client
- controllers
- auth.php // Will have methods like signin(), signout(), ...
- models // This will hold models you don't need in Admin module
// other models should be in the default models folder, so each module will be able to access them.
The routes would look like this:
$route['default_controller'] = "admin/user/signin"; // admin module, user controller, signin method
$route['admin/signin'] = "admin/user/signin";
$route['admin/(:any)'] = "admin/content/$1"; // admin module, content controller, (:any) method (content being an example, I have it in my CMS project)
$route['client/auth'] = "client/auth/login"; // client module, auth controller, login method
I solved it..In config file, I have added the following line of code;
$config['modules_locations'] = array(
APPPATH.'modules/' => '../modules/',
APPPATH.'modules/admin/' => '../modules/admin/',
APPPATH.'modules/client/' => '../modules/client/',
);
It works like a charm..:)
Today I started using Codeigniter+the modular extension from wiredesignz(hmvc) and I have a lot of unanswered questions but for the moment I will stick only to 1 .I understood how the modules work and I want to create a test website where I have 3 modules ( home,about and blog ) .
My folder structure :
Home
|-controllers
|-models
|-views
About
|-controllers
|-models
|-views
Blog
|-controllers
|-models
|-views
Now the question : How can I make the home module to be seen as homepage/main page ? I tried with config
$config['base_url'] = 'modules/home'; it didn't work
I tried some tweaks from the main index file but unfortunately it bricked my code.
I tried with htaccess but nothing .
Thank you .
Each module may contain a config/routes.php file where routing and a default controller can be defined for that module using:
$route['module_name'] = 'controller_name';
Taken from wiredesignz.
Kiran above was bit close. I tried to do so on live environment but failed. Apparently, this would work only if your default module and default controller are same (that is, home). Otherwise, you'll have to specify the module name as well as the controller name. In my case, I specified module name and controller name in default controller and it picked up!
$route['default_controller'] = 'admin/home';
In your case, it would be:
$route['default_controller'] = 'home/controller_name';
It might help to you, if still you are waiting for answer, Change your default controller to "home" controller.
In config/routes.php
$route['default_controller'] = 'home';//home is the controller name of home module.
just go to your application\config\routes.php and change your default controller to the module and controller you want:
$route['default_controller'] = 'module/controller_name';
I have some troubles with routing in Codeigniter framework. I'm in desperate need of finding regexp that will recognise string in URL when there is no 'admin' included
Information about enviroment:
Codeigniter Framework
i18n Multi language Library Helper (Language is changed when en/ ru/ fr/ is added to URL)
HMVC modules enabled with main and admin modules
Routing options:
1. $route['default_controller'] = "main";
2. $route['(\w{2})?\/admin\/?(.*)'] = 'admin/$2';
3. $route['(\w{2})?\/?((?!admin).*)'] = 'main/$2';
4. $route['(\w{2})'] = $route['default_controller'];
5. $route['404_override'] = '';
Describing the problem with routing behaviour.
I want to access admin module by adding "admin" to URL address (this works fine)
I want to access admin with language indicator like "en/admin" (this works also fine)
I want to access "main" module without writing "main" in URL address. Instead of http://somewebsite.com/main/controller/method I would like to use http://somewebsite.com/controller/method
Summary
In my routing configuration I need regexp which will launch "main" module when there is no "admin" word in URL address. This should also work with internalization.
I've alredy came up with (\w{2})?\/((?!admin).*) but it works only when I add language to URL like "somewebsite.com/en/some_controller/(...)" because "/" is recognised.
I also tried (\w{2})?\/((?!admin).*)|((?!admin).*) but there is third group added so the "main/$2" rule will not work ("main/$3" should do the trick but it doesn't)
Any help would be appreciated, thanks
This following settings for Routing file works for me. Please note the order of the rules
$route['default_controller'] = "main";
$route['(\w{2})'] = $route['default_controller'];
$route['admin'] = 'admin';
$route['(\w{2}\/)?admin\/?'] = 'admin';
$route['(\w{2}\/)?admin\/?(.*)'] = 'admin/$2';
$route['(\w{2}\/)?((?!admin).*)'] = 'main/$2';
$route['(\w{2})?/(.*)'] = '$2';
$route['404_override'] = '';
In my project, have setup an admin panel under a folder inside controllers folder like this
controllers/admin_panel/dashboard.php
And when we open it like this, it loads dashboard controller default
http://www.mysite.com/admin_panel
Now I have added a page controller on root level to load the page content from database. So here I have some a setup of kind of CMS. To load the page controller, I have added a condition in routes as below
$route[':any'] = "page";
But what it is doing now is, when I try to open admin_panel, it loads the page controller.
So I want to add a kind of exception condition here like route any except admin_panel
Any suggestions how can I achieve this ?
Thanks in advance.
You can just define another route right above that one that will take you to the admin panel. In CodeIgniter, routes will run in the order they are defined.
$route['admin_panel'] = 'admin_panel';
$route[':any'] = 'page';
You should be able to access the admin_panel with the above routing.