Problems with creating subfolders in CodeIgniter 3 HMVC - php

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..:)

Related

HMVC Integration in Codeigniter view paths

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);

Codeigniter 3.0 routing problems

I'm new in Codeigniter framework and got routing problem.
I have Main controller and Tournaments controller.
My routes look like this:
$route['default_controller'] = 'main';
$route['main'] = 'main';
$route['tournaments/results/(:num)'] = 'tournaments/results/$1';
When I go to localhost/tournaments/results/1 and then click to link "Main" I got localhost/tournaments/results/main instead of localhost/main
What is wrong with my routes or maybe problem is somewhere else?
Probably your link to 'Main' is wrong, not your routes files. Check your link.

Codeigniter HMVC to set up custom routes for modules

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.

codeigniter how do i setup routing for controller class and method?

I am new to CI and need some beginner's help from experts.
Here is what my current setup is:
/controllers/
home.php
report.php
/views/
home/index.php
home/recent.php
report/index.php
report/generate.php
the URI i am trying to produce as an outcome:
http://localhost
http://localhost/report (would load the index.php)
http://localhost/report/generate (would call the method for generate in the report controller)
http://localhost/recent/10 (would call the method for generate in the home controller passing the variable '10')
$route['default_controller'] = "home";
$route['404_override'] = '';
$route['/'] = 'home/index';
$route['recent/(:num)'] = 'home/recent/$1';
$route['report/(:any)'] = 'report/$1';
How do i avoid always modifying the routes file for each new method created in a class? so that it would follow:
$route[$controller/$method/$variable] (very use to how .net mvc routing is setup).
Any help is appreciated.
You don't need further modifications. In fact, even this line is redundant:
$route['report/(:any)'] = 'report/$1';
This one is also redundant:
$route['/'] = 'home/index';
since the default controller is set to 'home' and the default method is always index.
Look at how CI works with URLs: https://www.codeigniter.com/user_guide/general/urls.html
So, /localhost/report/generate would look for the Report controller, and load its generate method. That's how it works out-of-the-box, no routing needed.
And this route is fine:
$route['recent/(:num)'] = 'home/recent/$1';
If will take the URL /localhost/recent/123 and will load the Home, controller, recent method and will pass 123 as the first method parameter.

How to route codeigniter routes for accesing users like in Facebook (www.facebook.com/username)

I'm using codeigniter framework and by default i load my site controller
$route['default_controller'] = "site";
But now i want to setup the routes.php config so i could still access my controllers as before but if controller doesent exists then i'd like to run users controller and check if it's nickname so i can display that users profile.
It's something like on Facebook where you can have www.facebook.com/username and it takes you to your user profile. But i'd like that my other controllers are still accessible www.mysite.com/site, www.mysite.com/site/function, etc
I have tried with :any wildcard but couldnt get it working. I have seen that some solve the problem with regex expression but i'm not good at regex and dont know how to put it together so it would work for me.
Try my way:
First, Set $route for each controller that you have, exmp:
$route['controller_1'] = 'controller_1';
$route['controller_1/(:any)'] = 'controller_1/$1'; //this let you access your method
$route['controller_2'] = 'controller_2';
$route['controller_2/(:any)'] = 'controller_2/$1'; //this let you access your method
Next, use :any
$route['(:any)'] = 'user_controller/$1';
good luck !!!
Try this way. it will reduce a lot of repetitive line if you have lots of controller but i don't know does it violate any CI rules.
//this code block should be placed after any kind of reserved routes config
$url_parts = explode('/',strtolower( $_SERVER['REQUEST_URI']) );
$reserved_routes = array("controller_1", "controller_2", "controller_3" );
if (!in_array($url_parts[1], $reserved_routes)) {
$route['([a-zA-Z0-9_-]+)'] = "controller_1/profile/$1";
}

Categories