Is it possible to create such urls in codeigniter?
http://site.com/shorturl/
Where shorturl isn't a physical controller file, but a variable.
I expect the algorythm for parsing url query to be like this:
1) Search for physical controller file. If exists, do standard codeigniter routine. If not
2) Try to load special controller file, where "shorturl" is a variable. Do further stuff inside that controller.
Thanks in advance
The previous answer seems quite good, but thought I'd share what I'd though of.
If you set your 404_override to point to a controller you have set up as follows:
$route['404_override'] = 'welcome/short';
Any URL that doesn't exist (any short URL for example) would get sent there, where you could do the following to check the value:
public function short() {
$shortCode = $this->uri->segment(1);
}
That would give you the value you need to check. If all is well, do the redirect, if the code doesn't exist, you can then use the show_404 method to actually show the 404 page.
Related
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';
I'm relearning PHP and codeigniter but i can't help but notice how ugly my url looks.
So i have a pages controller: Say lets load homepage.
It would look like this:
public function home(){
$this->load->view('template/header');
$this->load->view('template/navbar');
$this->load->view('pages/home');
$this->load->view('template/footer');
}
I guess thats decent as my url will probably look like this:
http://localhost/ProjectName/index.php/pages/home
But whenever i click on a link that involves more data processing for example lets say my home has a list of users and i click on one to view their profile. I ofcourse have to pass something to identify which profile i am to load so my link would look like this:
http://localhost/OnlineStories/index.php/pages/view_userprofile/1
In my controller id have:
public function view_userprofile(){
$userid = $this->uri->segment(3);
//process data then load view
}
I'm not asking for any advanced stuff but i would like to ask simple things to improve my url like correct standard for naming my controller functions.
For example: on previous instances of loading my homepage my function name was view_home now its simply just home so its cleaner.
Maybe i could rename my view_userprofile to simply user_profile.
Anyway is there any better method to passing values from view to controller other than uri segment? its what ive been using so i never gave it much thought. I've seen some use get() but idk if its better than uri segment?
Or should i stick with the uri segment and keep my url as controller/method/parameter?
I haven't really explored post-project polishing in codeigniter so idk if theres a way to polish/change the url(just the name).
Talk about URL in CodeIgniter, you never mention about application/config/routes.php file. Have you seen it ..?
In routes.php file you can create your URL name and mapping it to the controller/function that you have
like this:
$route['user/update'] = 'user_controller/update_data_function'
so, you can call URL http://your.site/index.php/user/update to execute your update_data_function() in your User_controller.php
I have the following (basic) route set up in a CI-based web app:
$route['sms/resend/(:num)/(:any)'] = 'sms/resend/$1/$2';
The controller + 'resend' method:
class Sms extends CI_Controller {
public function resend($to, $message) {
// my code
}
}
Logically speaking, anything that doesn't fit the route should be directed to a 404 page instead of the resend() method within the sms controller. This isn't the case, however. The following URL, for example, isn't redirected correctly, it goes to the same controller+method:
http://myapp/sms/resend/uuuu/WhateverMessage
What could be the problem?
After a bit of digging, I've come to understand that CI's default routing does not get deactivated when a default route related to a specific controller/method pair is added. That being said, if a URL does not fit the route $route['sms/resend/(:num)/(:any)'] = 'sms/resend/$1/$2', then the same URL is run through CI's default routing mechanism as a fallback, so it still takes me to the resend method of the sms controller. To prevent this from happening, I needed to add another custom route that follows all others related to the sms resending, that redirects any other url to a different controller+method. If this controller doesn't exist, you get the default 404 page.
So the final /config/routes.php file:
$route['sms/resend/(:num)/(:any)'] = 'sms/resend/$1/$2';
$route['sms/checkoperator/(:num)'] = 'sms/checkoperator/$1';
$route['sms/(:any)'] = 'somewhereovertherainbow';
I think the rout file is just for rerouting. Your URL don't fits the routing Conditions so it don't gets rerouted! So it goes the normal way wich is the same (in this case!)
Something like this could work!
(! :num) /(:any) '] = error page (or not existing page)
So every request wich doesn't start with a number gets redirected to the error page!
The syntax might be wrong!
This would work great :
$route['sms/resend/[^0-9]/(:any)'] = 'errorpage';
You have to replace the error page by something ;)
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 ?
How can I rewrite the following codeigniter url
localhost/test_project/pages/show_page/11
to
localhost/test_project/pages/11
and is it possible to further reduce like:
localhost/test_project/11
Thanks.
You should edit this file: application/config/routes.php.
Let's assume you have a pages controller and a show_page method with receives an id as a parameter (just as you said).
your URL is like localhost/test_project/pages/show_page/11
I assume that the parameter is always a number, so using wildcards as described here you can add the following to your routes.php file:
$route['pages/(:num)'] = "pages/show_page/$1";
So your URLs will be like localhost/test_project/pages/3. This is like saying if anyone tried to visit localhost/test_project/pages/3, he/she actually means localhost/test_project/pages/show_page/3.
For urls like localhost/test_project/3 you can add this:
$route['(:num)'] = "pages/show_page/$1";
You've a few options. These are in descending order of priority.
First there is routes.php, which is the generally preferred way of handling things (information already provided, but here it is again).
You can also add a _resolve method to a controller (which means that CI needs to get to your controller to begin with), and
finally you can override the CI_Router library, the class which actually returns the route to go to.
Seriously, don't override CI_Router unless you know what you're doing.