I have two controllers Configure.php and Users.php. In my routes.cfg I have:
$route['default_controller'] = 'Users/login';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['registration'] = 'registration';
$route['login'] = 'login';
$route['subit_backend']['GET']='subit_backend/register';
$route['save_userinput']='Users/save_userinput';
When a user brings up the website the following page comes up in the browser bar https://www.stantiation.com/sub_crud/Users/login/ which is perfect.
The problem is that if a user has forgotten their password I am having trouble routing them to method where they can create a new one. I have the user send an email to themselves where I have placed a "code" that will allow them to update their password. The email has a link to this form:
https://www.stantiation.com/sub_crud/Users/resetPassword?fp_code=492c8bbd3841xxx8201f3a01d77fd.
I also have a view file called view/Users/resetPassword.php which is form that allows the user to enter a new password. That pops up fine. This is the post method of the form.
When the user presses submit, I get https://www.stantiation.com/sub_crud/Users/save_userinput in the toolbar and a 404 error, because there is no save_userinput.php file. I am trying to get the save_userinput() method in the Users.php controller to run, not save_userinput.php in the view/Users/directory. I agree with the 404 because that file doesn't exist.
How can I specify in a form that I want the method in controller Users, not the file view/Users/save_userinput.php? That is why I put the last $route in but that doesn't seem to help.
It seems that you might be understanding the CodeIgniter URL scheme. Read about it HERE.
Basically, it boils down to http:doman.tld/controller/function[/var1 ...[/varN]]
So, after the domain you have multiple segments that are interpreted like so:
The first segment represents the controller that should be invoked.
The second segment represents the class function, or method, that should be called.
The third, and any additional segments, represent any variables that will be passed to the controller.
So the URL https://www.stantiation.com/sub_crud/Users/save_userinput would go to the controller Users (which appears to be in the folder of /application/controllers/sub_crud) and call the controller method save_userinput. It is not looking for a file named save_userinput.php.
The 404 could be because the controller is not in a sub-folder or because some other file the controller tries to load, i.e. a "view" file, cannot be found.
It's hard to offer better advice without seeing the html for the form and knowing exactly how you have your file layout structured.
(Side note: I avoid putting controllers in sub-folders because it messes with the URL "look" and IMO, in terms of "controllers", it's not that hard to track what-is-what.)
You really only need routes when you want to override CodeIgniter's segment-based approach to URLs. With that in mind, it appears (based on a general lack of understanding about your app) that the following "routes" don't make sense.
$route['registration'] = 'registration';
$route['login'] = 'login';
$route['subit_backend']['GET']='subit_backend/register';
$route['save_userinput']='Users/save_userinput';
Related
I'm trying to make my CodeIgniter application work similarly to WordPress.
I want to be able to make these kind of URLs:
http://www.example.com/my-post-example
http://www.example.com/new-headline-here
http://www.example.com/i-love-stackoverflow
My routing:
$route['(:any)'] = "core/index/$1";
Which will call my Core controller and pass the page name into the index function.
I then lookup in my database for the page name and display the page to the user. So far so good.
However, there will be times when I want to call another controller. For example:
http://www.example.com/admin/edit_page/3
http://www.example.com/admin/settings
Now I assume my route will just grab all these rules and send them into my Core controller. Is there a way to make an exception for certain pages? Or is it a good idea to do this check inside my Core controller.
For example,
if ($page not in DB) {
// Call controller/method
}
This seems a little redundant since I just want CodeIgniter to handle this.
The routing rule you using it is OK for your purpose.
If you use http://www.example.com/admin/edit_page/3 this link it will send you admin controller and edit_page method.It will not use routes any rule.
However you will get one problem if your link looks like this
http://www.example.com/my-post-example/test
It will try to go my-post-example controller and test method.
Again http://www.example.com/admin will use routes any rule, means it will redirect your to core controller instead of admin/index. In that case your url should be http://www.example.com/admin/index
Finally If you call your other link with controller/method name it will be OK using your any rule
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
I think this is a route issue but I'm not sure. I have a page with this URL:
siteurl.com/kowmanger/titles/titles/edit/$id
I'm trying to find out that when I'm on this page I load the titles page it says page not found so I need to tell it that the $id is just a paramter so I can use it to get the data of the title.
UPDATE :
So I decided to change my titles controller so that there's a edit and add function inside of the titles controller that way they dont' have separate controllers when they are in fact methods.
So now I have:
kansasoutalwwrestling.com/kowmanager/titles/titles - list of titles
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
kansasoutalwwrestling.com/kowmanager/titles/titles/edit/$id - edit form
I don't have any routes set up so far for this. For some reason though I"m getting the same page for both of these page.
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
(right link url) kansasoutalwwrestling.com/kowmanager/titles/add -
addnew form
I need a route so that it'll show the correct url if the add method is accessed.
Also I need to set up a route so that if the correct edit link is accessed it sees the id attached to the end of the url and it'll accept it so that I can do a my database query to get the title data.
UPDATE: So to reiterate I have a module(subfolder) called titles. Inside of the module I have a controller called titles and inside of that controller I have 3 functions called index(), add(), edit().
I tried using Chris's suggestion on the routes but its not routing correctly. Also wanted to mention I'm using wiredesignz modular separation framework if that matters.
Any additional ideas?
Possible answer based on your post, not one hundred percent your entire structure but if i had to guess based off the post I would try this as my routes first..
$route['titles/titles/edit/(:any)'] = 'titles/titles/edit/$1';
$route['titles/titles/add'] = 'titles/titles/add';
$route['titles/titles'] = 'titles/titles';
$route['titles'] = 'titles/index';
Are you using custom routing in your configuration files ?
The general routing protocol used by codeigniter is like this:
domain.com/controller/methode/param1/param2/param3
This being said, your url
siteurl.com/kowmanger/titles/titles/edit/$id
corresponds to something like this :
class Kownmanger extends CI_Controller
{
public function titles($titles, $action, $id)
{
}
}
In case you are using sub-folders in your controllers folder, what I have just said will change, Could you please tell us what's your directory structure ?
On my website, I am loading the content dynamically from database like this
e.g mysite.com/about-us
for this, there is an enrtry in database, so it will load the content for 'about-us' & print it using "page" controller only.
for this what I have done is, I have added below configuration in routes.php
$route[':any'] = "page";
but lets say if I already have controller named "about-us" and I want to load that & not the one from database, how can I do that?
A smooth solution would be to use the error/missing_page controller and point it in the config/routes.php.
Then it would automaticly pick all existing controllers first, and then that controller.
You can also call show_404() if you don't find a record in the database.
This allows you to create new controllers without having to point all of them in the route file.
Read about 404 override here
you need to add this
$route['about-us'] = "aboutus";
$route['about-us/(:any)'] = "aboutus/$1";
before
$route[':any'] = "page";
as the CI route is not greedy, it will not check for the page controller after it finds the about-us controller.
I have a problem with Codeigniter routes. I would like to all registered users on my site gets its own "directory", for example: www.example.com/username1, www.example.com/username2. This "directory" should map to the controller "polica", method "ogled", parameter "username1".
If I do like this, then each controller is mapped to this route: "polica/ogled/parameter". It's not OK:
$route["(:any)"] = "polica/ogled/$1";
This works, but I have always manually entered info in routes.php:
$route["username1"] = "polica/ogled/username1";
How do I do so that this will be automated?
UPDATE:
For example, I have controller with name ads. For example, if you go to www.example.com/ads/
there will be listed ads. If you are go to www.example.com/username1 there are listed ads by user username1. There is also controller user, profile, latest,...
My Current routes.php:
$route['oglasi'] = 'oglasi';
$route['(:any)'] = "polica/ogled/$1"
$route['default_controller'] = 'domov';
$route['404_override'] = '';
I solved problem with this code:
$route['oglasi/(:any)'] = 'oglasi/$1';
$route['(:any)'] = "polica/ogled/$1"
$route['default_controller'] = 'domov';
$route['404_override'] = '';
Regards, Mario
The problem with your route is that by using :any you match, actually...ANY route, so you're pretty much stuck there.
I think you might have two solutions:
1)You can selectively re-route all your sites controller individually, like:
$route['aboutus'] = "aboutus";
$route['where-we-are'] = "whereweare";
//And do this for all your site's controllers
//Finally:
$route['(:any)'] = "polica/ogled/$1";
All these routes must come BEFORE the ANY, since they are read in the order they are presented, and if you place the :any at the beginning it will happily skip all the rest.
EDIT after comment:
What I mean is, if you're going to match against ANY segment, this means that you cannot use any controller at all (which is, by default, the first URI segment), since the router will always re-route you using your defined law.
In order to allow CI to execute other controllers (whatever they are, I just used some common web pages, but can be literally everything), you need to allow them by excluding them from the re-routing. And you can achieve this by placing them before your ANY rule, so that everytime CI passed through your routing rules it parses first the one you "escaped", and ONLY if they don't match anything it found on the URL, it passes on to the :ANY rule.
I know that this is a code verbosity nonetheless, but they'll surely be less than 6K as you said.
Since I don't know the actual structure of your URLs and of your web application, it's the only solution that comes to my mind. If you provide further information, such as how are shaped the regular urls of your app, then I can update my answer
/end edit
This is not much a pratical solution, because it will require a lot of code, but if you want a design like that it's the only way that comes to my mind.
Also, consider you can use regexes as the $route index, but I don't think it can work here, as your usernames are unlikely matchable in this fashion, but I just wanted to point out the possibility.
OR
2) You can change your design pattern slightly, and assign another route to usernames, something along the line of
$route['user/(:any)'] = "polica/ogled/$1";
This will generate quite pretty (and semantic) URLs nonetheless, and will avoid all the hassle of escaping the other routes.
view this:
http://www.web-and-development.com/codeigniter-minimize-url-and-remove-index-php/
which includes remove index.php/remove 1st url segment/remove 2st url sigment/routing automatically.it will very helpful for you.
I was struggling with this same problem very recently. I created something that worked for me this way:
Define a "redirect" controller with a remap method. This will allow you to gather the requests sent to the contoller with any proceeding variable string into one function. So if a request is made to http://yoursite/jeff/ or http://yoursite/jamie it won't hit those methods but instead hit http://yoursite/ remap function. (even if those methods/names don't exist and even if you have an index function, it supersedes it). In the _Remap method you could define a conditional switch which then works with the rest of your code re-directing the user any way you want.
You should then define this re-direct controller as the default one and set up your routes like so:
$route['(.*)'] = "redirect/index/$1";
$route['default_controller'] = "redirect";
This is at first a bit of a problem because this will basically force everything to be re-directed to this controller no matter what and ultimately through this _remap switch.
But what you could do is define the rules/routes that you don't want to abide to this condition above those route statements.
i.e
$route['myroute'] = "myroute";
$route['(.*)'] = "redirect/index/$1";
$route['default_controller'] = "redirect";
I found that this produces a nice system where I can have as many variable users as are defined where I'm able to redirect them easily based on what they stand for through one controller.
Another way would be declaring an array with your intenal controllers and redirect everything else to the user controller like this in your routes.php file from codeigniter:
$controllers=array('admin', 'user', 'blog', 'api');
if(array_search($this->uri->segment(1), $controllers)){
$route['.*'] = "polica/ogled/$1";
}