I have encountered an issue using subfolders with Codeigniter + HMVC.
/system/application/modules/welcome/controllers/staff/welcome.php
To access this I have to access it via http://www.app.com/welcome/staff/welcome
Unfortunately this doesn't fit the rest of my URL structure.
How can I remove the first welcome from the URL so the URL is http://www.app.com/staff/welcome
I have tried adding a route within the module:
/system/application/modules/welcome/config/routes.php
like:
$route['staff/welcome'] = "welcome/staff/welcome";
but unfortunately no luck.
Adding this route to the real codeigniter route file works but I feel that adding code external of the modules to get modules to work misses the point of adopting modularisation.
I hope someone is able to help.
Thanks,
Tim
This line in your routes.php is correct:
$route['staff/welcome'] = "welcome/staff/welcome";
You can try to play with the order of your routing rules, and to put the rule for the default controller ($route['default_controller'] = "home";) at the end. I have project with 4 modules and it works fine for me.
Related
My controller structure (api is the folder inside controller)
controllers/api/Api_1_0.php
controllers/api/Api_2_0.php
In my routes.php
$route['api/(\d+)\.(\d+)'] = "api/Api_$1_$2";
$route['api/(\d+)\.(\d+)/(:any)'] = "api/Api_$1_$2/$3";
The routing strategy i need is, if /api/2.0 is specified it will point to controller file Api_2_0.php
ie. api/x.y points to file Api_x_y.php
Everything working fine with above routing but below is my issue:
https://www.example.com/api/2.0/photos/1234567890 // not working
https://www.example.com/api/2.0/photos // working
How to solve ?
As mentioned in a comment you should change the order.
But you should also change (:any) to (.*). (:any) would only match first segment of your url.
So correct way would be:
$route['api/(\d+)\.(\d+)/(.*)'] = "api/api_$1_$2/$3";
$route['api/(\d+)\.(\d+)'] = "api/api_$1_$2";
I managed to run default controller from sub directory by adding a MY_Router file in application/core, everything works fine but this is what I am facing now.I am unable to route it if url hits /admin or /student or /teacher which is eventually a sub directory in controllers.
https://s14.postimg.org/pr3ta38f5/controller_structure.png
https://s14.postimg.org/z05zk7hb5/error_1.png
https://s14.postimg.org/mmt5darmp/issue_2.png
https://s14.postimg.org/kwa4bta3l/page_controller.png
https://s14.postimg.org/j5voo2hy9/routes.png
If you folder structure is thus:
- controllers
- teachers
- Teacher_home.php
- students
- Student_home.php
- admin
- Admin_home.php
Then normal CI routing would be with URLs to the default index method would be like:
mysite/teachers/teacher_home
mysite/students/student_home
mysite/admin/admin_home
Your routes could then point mysite/teacher_home to the relevant controller above like this:
$route['teacher_home'] = 'teachers/teacher_home';
In the CI docs they describe the wild cards you can use: http://www.codeigniter.com/user_guide/general/routing.html
But any route must point to a valid controller/method url. Get your site/app working under normal default routing, then add the alternate routing in afterwards.
So remove your current routes. If you have a default route in it will probably be messing up your other routes if you have not written it properly, or in the right order.
Hope that helps,
I would like to add a new phtml file to my index folder in which I already have several views:
index
landing
And so on... I access them by using the following logic:
sitename.com/index/landing
or
sitename.com/index/index
How can I add the phtml file (my new view) to my index folder so that I'm able to see it when I enter in the browser:
sitename.com/index/mynewview
I'm quite new to the whole Zend Framework, and I'm not sure how the structure works exactly, so I'd like to find out more. Can you guys help me out with this, how am I supposed to do this?
Thanks heaps! :)
P.S. The views are in the following directory structure:
module/application/view/application/index/
and then here are all of the views, this is where I'd like to add my new view and access it from browser like this:
/index/testview
Edit:
When I add the testview.phtml to the index directory and put some test tags like this in it:
<h1> Testing new view page </h1>
It's not being rendered on the browser
Because this is an MVC framework, you're skipping a few steps. You're probably going to get a few harsh responses, but I'll try to fill in the holes for you very quickly.
Ignore the file folder structure for a minute.
This is a route:
/index/landing
Routes point to Actions inside of Controllers to work.
Assuming you have started with the skeleton, open up your module's module.config.php, you should see route config, e.g.:
https://github.com/zendframework/ZendSkeletonApplication/blob/master/module/Application/config/module.config.php#L29
You'll need to add a config entry for the routes you want to serve. It could be as simple as a Literal entry for /index/landing, or something more complex (Segments, Regex, etc.) that handle patterns for routing. Spend some time tinkering and learning here; routes are pretty critical to working with MVC.
When configuring the route, the assumption is that you have a Controller set up, and that this Controller has an Action (to which your route is pointed). That Action, is where you can connect template files (phtml,twig,etc.) to routes:
// dummy action that serves index/testview
public function fooAction(){
$vm = new Zend\View\Model\ViewModel();
$vm->setTemplate('index/testview');
return $vm;
}
That index/testview, will be in your module's view templates, not in your public folder.
I think that's a reasonable primer to get you on your way!
Take some time to learn:
http://zf2.readthedocs.io/en/latest/index.html#userguide
Maybe start here:
http://zf2.readthedocs.io/en/latest/in-depth-guide/understanding-routing.html
ZF2 (V3 is coming!) is a beautiful thing, it's worth it.
Good luck.
I want to manage my url in codeigniter...
where my frontend should be like this
"http://www.Himalayi.com/" and the
admin panel should be http://www.Himalayi.com/__admin
I am not able to manage though i've tried alot..
the folder structure is given below
Please
Help me.. Thanks
application/
controller/admin/home_banner.php,user.php
model/admin/model_home_banner.php, model_user.php
view/admin/home_banner/add.php
and
in the same folder controller i have
frontend folder the file is welcome.php!
how to manage in config, routes, htaccess
I do not know why you would want to prepend underscores to access the admin panel. Please keep in mind that defining controller functions like:
public function _notReachable(){
#do something
}
will result in an inaccessible function regarding web requests.
So calling example.com/home/_notReachable will not work.
Maybe that already solves your problem?
See codeigniter documentation "Controllers - private Functions".
I need to create a dynamic url in codeigniter like the facebook application. Is it possible to create such url using the codeigniter framework?
eg:
1. www.facebook.com/nisha
2. www.facebook.com/dev
You need to set up custom routing for the controller in application/config/routes.php. Like:
$route['([a-zA-Z]+)'] = "controller_name/function/$1";
This makes urls like the way you want, but it makes all of your controller inaccessible, that is because any '/controllername/parameter/' format will match with '(:any)' and will be redirected to our 'controller_name/function/'.
To stop controllers redirected by the CI router, you will have to explicitly define all of your controllers on the routes.php first then add the above mentioned routing rule at last line. Thats how i made it to work.
Hope that helps you in some way.
Its pretty easy to setup this by the use of routes. Read their routing guide
$route['([a-zA-Z]+)'] = "controller/user/$1";
However, if their is only one way of accessing the website, is like domain.com/username then its ok, otherwise, this will prove be a hard catch on the long run. On that case, limit the Route to a limited scope like
$route['users/([a-zA-Z]+)'] = "controller/user/$1";
This will help in the extending the system in numerous way
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";
}