I have already read a bunch of the articles on stackoverflow about this topic, such as:
CodeIgniter: SEO friendly URLs
Codeigniter routes not working sometimes
And I swear I have set up everything correctly, but after I put the route in and save my app, and attempt to go to the new URL, or even the old one, they both give me a 404 error.
I have an extension that currently looks like this:
search/map_view/county
that I want to look like this:
map/county
I wrote the following reroute in the routes.php file, which gives me the 404 error:
$route['search/map_view/(:any)'] = 'map/$1';
And just in case I was doing it backwards, I also tried it like this:
$route['map/(:any)'] = 'search/map_view/$1';
That didn't do anything, so I've deduced i did that incorrectly. A thing of note is that I do have apache's mod_rewrite changing my url's to drop the index.php from it. Don't know how that's helpful, but I've noticed it a lot in the other posts.
Am I supposed to change something somewhere else for this? I'm assuming that if I type in the previous address, I should get automatically rerouted to the new one? Or if I type in the new address, it should work automatically? I don't know, it's getting really annoying...
Anyhow, I have a lot of questions about this stuff, but I'm going to start here and then see if I can find the rest of the answers here after I fix this one.
EDIT - I've been asked to include more info. Here it is.
Here's the .htaccess content
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Currently I don't have any custom routes defined in the routes.php file, just because I can't get it to work correctly.
The current controller is "Search", with the method "map_view" being passed a variable "county". So the url would be
http://www.base_url.com/search/map_view/county
I want to change this to
http://www.base_url.com/map/county
Everything else I've previously written still applies. Thanks again!
You want your url looks like map/country .
In your routes.php
$route['map/(:any)'] = 'search/map_view/$1';
$route['map'] = 'search/map_view';
And be sure your controller name is Search.php.Also class name is Search that extends CI_Controller and method name map_view() (must be public function)
Look CI Controller Guide for detailed information
Related
Hi I'm currenly playing around with PHP MVC programming, and was wondering if anyone has made some sort of "routing" with a database?
I have a "page" table that looks like this: http://i.imgur.com/xS1OvjW.png
Currently all routes are hardcoded, and I thought it must be possible to do with a database..
But not only do I want to get the page and show it, I also need to be able to send parameters with it.
Example:
As shown in my page table, I have a "test" url. If i type http://demo.com/test/ I would get "rerouted" to use the "home" controller and "Index" method. But I also need to be able to type http://demo.com/test/id/40 and id/40 will be sent as params to the controller/method.
If this isn't a good thing to do, or if anyone got a better soloution please let me know! :)
Regards,
Frederik
This definitely depends on the server you're using, but since you're a PHP MVC noobie I'll reference apache in this example, and hope it's what you have for the sake of the examples.
First, you'll need your webserver configured to know that it has to send all traffic through your base page (usually index.php). Now that page would do some other stuff (call bootstrap, etc) but for the sake of argument we'll say that all it does right away is look at the request from the page, compare it to the DB, and complete the request if it can.
In that case, it will be helpful to have the request info from the server passed in to the index.php page. To do this, you'll want to configure apache with an .htaccess file similar to:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ public/?path=$1 [L,QSA]
This tells it to use index.php for all requests that aren't specific files or directories, and to pass the full url path through to index.php as a $_GET var.
Next, in index.php, you'll want to check the path you were passed against DB of paths. Here's a really simple example to show:
<?php
// Obviously use your database and some string parsing here to match correctly
// I generally explode the path on '/' to break it into controller, action, etc.
if ($_GET['path'] == "user/account") {
// Then you call the controller that matches the first part of the route
// The action that matches the second part of your route
// And pass the request along so you can access anything else
call UserController::accountAction($_REQUEST);
} else if ($_GET['path'] == "user/resetpassword") {
call UserController::resetPasswordAction($_REQUEST);
}
From there, you should be in the right place and have everything you need. That Controller/Action URL format is a fairly common one for how easily it lets us do this.
Hope the answer helped!
I am creating a project with Code Igniter as a back end framework and Bootstrap 3 as a front end framework.
I'm having an issue with accessing my pages via directly calling the controller followed by the method.
For example my controller is site.php and the method is home.
Here is what is looks like.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_controller {
public function index(){
$this->home();
}
public function home(){
$data["title"] ="SmartAgent";
$this->load->view("site_header");
$this->load->view("content_home", $data);
$this->load->view("site_footer");
}
As I understand the method index basically sets the method home as the index page.
When I type the web address in my url such as:
examplesite.co.uk
The controller correctly loads my view for the home method, which is content_home.php and the site loads the homepage along with the title fine.
However if I type:
examplesite.co.uk/site/home
This does not work! And I do not know why, this is further causing me issues such as URL's not working etc etc. However base url is set, and I can load CSS, JS, and image files fine. Also I have enabled helpers, routes and all else.
The above url works to load another project I was working on. So is why I know I'm missing something.
Any ideas anyone?
Thanks
Codeigniter routing is done relative to the index.php.
So your link should be examplesite.co.uk/index.php/site/home.
If that is the issue, then you need an .htaccess file, and in it write
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
(If I am not mistaken, writing from my phone)
Then, you will remove index.php from your site.
With codeigniter, you need to set $route[]. This is in application/config/routes.php
Also check out the codeigniter documentation on this, its pretty good and will explain it all.
Thank you everyone for your help and answers!
This was a silly mistake of mine and I wanted to let everyone know so that if anyone was to face this issue this may help. #Alexey answer above gave me a light bulb moment! So thank you.
Basically within the .htaccess mod rewrite file which can be downloaded from google. I forgot to change the directory for my server which is located at the top on line 4
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /yourfolderdirectoryhere/
If you are unsure or unclear please watch below tutorial which helped me.
https://www.youtube.com/watch?v=dynPx1B0jis
I'm quite a rookie with CodeIgniter, and as per title, I have troubles trying to setup a single controller for my application. It's a very simple static site with couple of pages like "home", "about" and so on...
I have this in my routes.php file:
$route['default_controller'] = "mycontroller";
$route['404_override'] = '';
$route['(:any)'] = "mycontroller/$1";
And this in mycontroller.php file:
// Home
public function index()
{
$data['page'] = 'home';
$this->load->view('template',$data);
}
// about
public function about()
{
$data['page'] = 'about';
$data['title'] = 'About Us';
$this->load->view('template',$data);
}
I'm working in a localhost environment, and the CI project is in this folder:
http://localhost/local/project/ci-tbs/
and I've specified it also in the config.php file for the base_url parameter.
Now what I'd expect pointing the browser to
http://localhost/local/project/ci-tbs/about
is to find the "About Us" page, instead I got a 404 error. Pointing to the base address corectly gives me the "Home" page.
What am I doing wrong?
Is it sensed to use a single controller istead of 1 per page? I'd totally do that in a quick way to fix, still I'm quite baffled by the fact that I can't understand what I am doing wrong and why it's not working. I'd like to simple set everything in one controller, one method per page.
I've already seen this topic asked here in SO, like using regular expressions in the route $route['(.*)'] = "mycontroller/$1";, but nothing really worked for my case wich I think is quite basic (so basic I'm sure my error is so gross that it will be quite embarrassing :P ).
Additional info:
I have in the folder an .htaccess file picked as is from the Html5 Boilerplate, tried with and without it but 404 is always there. I'm using XAMPP as local environment.
For answer
As mentioned by #Vincent Decaux in the answer, the deal to fix this was to add index.php in the url, the other interesting part is
Create your .htaccess file to "hide" index.php
This way I've resolved another small issue for the pages with missing findings for the assets files, so I used the following rule in the .htaccess file, redirecting all requests to the index.php file and excluding files in assets folder and images, along with robots.txt as suggested here https://stackoverflow.com/a/11846150/1262357
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
hope this helps others with same problems I had!
As mentionned in my comment, it seems to work using :
localhost/local/project/ci-tbs/index.php/about
Create your .htaccess file to "hide" index.php.
I'm not sure how to explain this problem so the title is kind of vague!
Well here I go, I'm working on a picture/album page on my website and everything is working great. But I want to add a next/previous picture feature like any good picture website has. I want to passe the different options for sorting out the album by variables.
For now the url on a picture is http://localhost/photo/174/picture-name/, I would like to add on this some parameters so that the url then looks like http://localhost/photo/174/picture-name/album:5/sort:name/.
With the help of .htaccess I would like to extract the variables album and sort`.`But the little catch is that I would still want to be able to get to the page with only this urlhttp://localhost/photo/174/picture-name/``
For now my .htaccess file looks like this :
RewriteEngine on
RewriteRule ^photo/([A-Za-z0-9]+)/(.*)/$ photo.php?pic_id=$1
I tried adding this line in it but it did not work out.
RewriteRule ^(.*)/album:([A-Za-z0-9]+)/sort:([A-Za-z0-9]+)/$ &album=$2&sort=$3
I hope someone has an answer for me,
Have a good day
Joris
Did you mean something like this?
RewriteRule ^photo/([A-Za-z0-9]+)/[^/]+/album:([A-Za-z0-9]+)/sort:([A-Za-z0-9]+)/$ photo.php?pic_id=$1&album=$2&sort=$3
You also need to change your original rule:
RewriteRule ^photo/([A-Za-z0-9]+)/([^/]*)/?$ photo.php?pic_id=$1
Because what you have will match against the URI with the parameters
I guess you are talking about this, and your example wasn't correct:
RewriteRule ^photo/(.*)/.*/album:(.*)/sort:(.*)/$ photo.php?pic_id=$1&album=$2&sort=$3
(for simplicity, I replaced some of the expressions).
Here, http://mydomain.com/photo/174/picture-name/album:5/sort:name/ get rewritten to http://mydomain.com/photo.php?pic_id=174&album=5&sort=name .
I am working on creating page links from DB like the following example.
Current page:
www.example.com/page.php?pid=7
In the DB it is saved as title "contact-us" under category "Company Info"
I want it to be like:
www.example.com/company-info/contact-us.html
I have tried different solution and answers but did not got any luck. I am not sure, where will be the PHP part and which rules to write for .htaccess files.
In apache (or .hataccess) do something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /proxy.php?_url=$1 [QSA,L]
So in a nutshell, if the resource being requested doens't exist, redirect it to a proxy.php file. From there $_REQUEST['_url'] will be the url the user was requesting.
Then create proxy.php in your home directory and add whatever logic you'd like to load the correct content.
If you use this from .htaccess, then you may need to add RewriteBase / to your config.
If you want to find this page by url, you will probably do this through php and .htaccess. Make a .htaccess that calls page.php for each and every request. You don't need the pid=7, because, well, how should the .htaccess know it is 7, right? :)
In page.php, you take the original url and split it on the slashes, so you get the category (company-info) and the page itself (contact-us.html). Then, you can look these up in the database. This is in a nutshell how many software works, including Wikipedia (MediaWiki) and CodeIgnitor.
Mind that 'company-info' isn't the same as 'Company Info'. You'll have to specify the url-version in the database to be able to use it for look-up.