Codeigniter 3.1.0 routing issue - php

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,

Related

Routing issues with custom route in Codeigniter

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

cakephp 3 prefix routing

I'm trying to set up a routing prefix in cakephp 3 so any URLs starting with /json/ get the prefix key set and I can change the layout accordingly in the app controller. Other than that, they should use the usual controller and action. I have added the following to routes.php
$routes->prefix('json', function($routes) {
$routes->connect(
'/:controller/:action/*',
[],
['routeClass' => 'InflectedRoute']
);
});
I want to direct all requests with json as first url segment to controller specified in second url segment. e.g. /json/users/add_account_type/ goes to users controller. However when accessing this URL I get the message:
Error: Create the class UsersController below in file:
src/Controller/Json/UsersController.php
whereas I want it to be using
src/Controller/UsersController.php
I think this should be possible but I can't quite see what I'm doing wrong when consulting the book. Have partly based my code on: CakePHP3.x controller name in url when using prefix routing
Thanks a lot in advance
That's simply how prefix routing now works in 3.x, as explained in the docs, prefixes are being mapped to subnamespaces, and thus to separate controllers in subfolders.
http://book.cakephp.org/3.0/en/development/routing.html#prefix-routing
If you'd wanted to change that behavior (I don't really see why), one way would be to implement a custom ControllerFactory dispatcher filter.
http://book.cakephp.org/3.0/en/development/dispatch-filters.html
On a side note, the RequestHandler component supports layout/template switching out of the box, so maybe you should give that a try.
http://book.cakephp.org/3.0/en/controllers/components/request-handling.html
http://book.cakephp.org/3.0/en/views/json-and-xml-views.html
Prefix routing is a way of namespacing parts of your routes to a dedicated controller. It seem that what you want is a scope and not a prefix, for what you describe:
Router::scope('/json', function($routes) {
$routes->fallbacks('InfledtedRoute')
});

In Zend, How Can I Route Controller-Omitted URLs To The Index Controller

In order to have user-friendly URLs in my application, I want all URLs that call actions in the index controller to omit the controller name. For example:
/user/edit => /user/index/edit
The "index" part of the URL is unfriendly. But, I would like other controllers in the module to work as expected, such as:
/user/article/publish (does not route to /user/index/article/publish)
I am using a config file (/config/route.ini) which is loaded into the bootstrap of Zend 1.12, which basically uses Zend_Controller_Router_Route, like so:
routes.index.route = :module/:action
routes.index.defaults.controller = index
routes.index.defaults.action = index
My stumbling block is that I could not make the routes work with Zend_Controller_Router_Route. These routes should work when the page is not found. If there is an existing action in an existing controller that the URL points to, it makes sense to go there instead.
Final thought: is this even possible within Zend? Maybe I should go with the .htaccess and use mod_rewrite?

CodeIgniter - Stuck with routes

I am trying out CodeIgniter 2.1.4. I already have a controller for showing static pages which I built using the tutorial in CodeIgnitor documentation. I later set-up my routes like this:
// <http://localhost/> refers to <http://localhost/pages/view/>
$route['default_controller'] = "pages/view";
// <http://localhost/somepage/> refers to <http://localhost/pages/view/somepage/>
$route['(:any)'] = "pages/view/$1";
// .htaccess is already setup to rewrite the url without index.php
Now, I don't have much experience with PHP, and the concepts of URL Rewriting and MVC Architecture are fairly new to me.
Let's say there's are pages called •Home, •About, •Admin and •Contact.
For the pages •Home, •About and •Contact, the Pages controller works right as it should.
But for •Admin page, I want to have a separate controller which determines whether the user has logged in or not, and whether he has admin rights etc. And if he hasn't logged in yet, I should load the Login view instead of the Admin view.
The Pages controller has a fairly simple logic. It checks whether the string in argument, appended with .php and prepended with the views directory, exists as a file or not. If it doesn't show_404(), if it does, load view header-template, then load the page, then load view footer-templat. I'm pretty sure most of you who have worked with CodeIgniter must've seen a similar logic for static pages.
I could do redirect('login') inside my Admin view, but that doesn't seem to work. If I create a separate controller for Admin, how would I access it, while according to routes, every URL gets directed to pages/view controller (line#4 in the above code).
As I've already said, I'm fairly new to this. It might be some retarded mistake that I'm making. Or my whole MVC structure might be inappropriately built. How do I get past this and start worrying about the authentication stuff? Can anyone advise?
$route['default_controller'] = "pages/view";
$route['admin/(:any)'] = "admin/$1"; //(admin controller) with "any" method
$route['(:any)'] = "pages/view/$1";
localhost/poject/admin/edit as example
The problem you are experiencing is simple, you overwrite all controllers by that (:any) it isn't wrong but you need to manualy assign each controller that you want route as normal controller as I posted above.
please note that routes are order dependant and if one (first) is used second one is ignored. "Routes will run in the order they are defined. Higher routes will always take precedence over lower ones."
For authentification please see this post.
I would rather use _remap() and extend CI_Controller to take over my routes instead of having this route $route['(:any)'] = "pages/view/$1"; in routes.php.
remapping

Problems with Codeigniter HMVC subfolders

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.

Categories