URL mapping can be done at .htaccess and we can edit routes.php. Learning PHP/Codeigniter, i posted this question. CodeIgniter Mod Rewrite Rules and the Controller
routes.php worked for me, and the next thing i'd like is to have access to the GET parameters in my URL. To elaborate on what i'm actually trying to do:
Case 1:
RewriteRule ^([a-z]+)/(at)/([a-z]+)$ index.php/person/show?personName=$1&place=$2 [L]
This doesn't work at the .htaccess as in the above question, but I can get that working in the routes.php
$route['([a-z]+)/at/([a-z]+)'] = "person/show/$1/$2";
If i try the routes.php as
$route['([a-z]+)/at/([a-z]+)'] = "person/show?personName=$1&place=$2";
I end up with a CodeIgniter 404. Is this fundamentally incorrect?
This works fine as is:
person/show?personName=chinmay&place=kino
Case 2:
URL which I want to expose:
chinmay?place=kino
where place is shown as a GET param. I would like to handle this, and knowing as much about routes.php-
$route['([a-z]+)\?place=([a-z]+)'] = "person/show/$1/$2"
This gives a CI 404 as well.
Passing URI Segments to your Functions
or
GET parameters in the URL with CodeIgniter
You could use:
<?php
function myFunction($param1, $param2){
//code
}
?>
And access it like: .../controller/myFunction/param1/param2
Related
On my local server, I have setup a website using Codeigniter which opens up fine using below URL
I have a different kind of query regarding rewriting URL
http://localhost/mywebsite
Actually the real URL is below
http://localhost/mywebsite/page/
Which I want it to work as root url as
http://localhost/mywebsite
So I have done it by adding it as default controller in config.php
But now I have another URL as
http://localhost/mywebsite/page/mypagename
Which should open as
http://localhost/mywebsite/mypagename
How can I do it?
You can define a custom route in /system/application/config/routes.php - for example:
$route['abc'] = 'controller_name/abc';
Then, http://mydemosite.com/abc
goes to http://mydemosite.com/controller_name/abc
see https://ellislab.com/codeigniter/user-guide/general/routing.html for more information of URI Routing.
I hope this can be helping you.
I got it solved using routing method as below under config/routes.php
$route['([^/]+)/?'] = 'page/$1';
i want to build a website with different views, but a stable header and footer - no problem so far. But i dont like the kind of urls i got at the moment with the php GET method.
My site at the moment works like this (what istn working properly):
$_page = $_GET['p'];
if ($_page == "city-sitemap"){ include "views/city-sitemap-view.php"; }
if ($_page == "place"){ include "views/place-view.php"; }
else { include "views/index-view.php";}
this isnt a very sweet solution but i dont know a other for now. I tried to use a mvc framework but failed dramatically. So everytime i add a link i use for example this "index.php?p=place" - not very nice.
The including of the views isn very smart as well? is there a better way?
I would like to use something like the rewriteEngine that the new url is like a folder.
Can you help me to find a better solution?
Thanks a lot
Page including
For the page inclusion, you can use a simple array to dynamically allocate your page to a specific name. As so:
$pages = array('city-sitemap'=>'views/city-sitemap-view.php',
'place'=>'views/place-view.php',
)
if(array_key_exists($_GET['p'], $pages){
include $pages[$_GET['p']];
}else{
include 'views/error.php';
}
This array should be added in a general configuration file. With this configuration if you want to display your city-sitemap-view.php view, you will have to write this url: http://www.domain.com/index.php?p=city-sitemap
Url rewriting
It is possible to rewrite an URL with a .htaccess file. Here is an example of code you would can to write in your .htaccess file.
RewriteEngine On
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]
An url that looks like this:
http://www.domain.com/index.php?p=city-sitemap
will be converted to this one:
http://www.domain.com/city-sitemap
There is something called the front controller that you should look into. You can write a very simple one yourself. It works in conjunction with URL rewriting. If for example your url looks like this:
/mypage.html
the url rewriter will reroute the request to index.php .
Index.php will then look at the URL and do somehting like this:
1) break the page out into string, ignoring .html extention
preg_match("\/(.*?)\.html", $_REQUEST['URI'], $matches);
$pageClass = $matches[1];
//$pageClass = "mypage"
2) look for and load a class named "Mypage.php"
$controller = new $pageClass();
3) call the run method on the page class, passing it all the request parameters
$request = new Request($_REQUEST);
$controller->run($request);
you can then do all the page specific stuff inside your controller class that is specific to the page.
At each simple step along the way, you will find you want to do more and more things like authentication, filtering, tracking, etc. You will get end up developing a front controller that is specific to your needs, as well as a base Controller class that does a bunch of standard stuff that all your controllers have in common.
As per my comment, i really think you should consider the framework i linked, (it is very logical) or any other micro framework, but if you really wish to do this yourself then you can handle your includes like so:
<?php //index.php
$requested_page=isset($_GET['p'])?$_GET['p']:'home';
//maybe have this included from pages.php for organization
$pages=array(
'home'=>'home_content.php',
'about'=>'about_content.php',
'contact'=>'contact_content.php'
);
include "views/header.php";
if(array_key_exists($requested_page, $pages)){
include "views/".$pages[$requested_page];
}else{
header('HTTP/1.0 404 Not Found');
include "views/error404.php";
}
include "views/footer.php";
This keeps your pages in a single array, and protects against arbitrary inclusion vulnerabilities.
For nicers urls, see The other users .htaccess rewrite rules
On a codeigniter installation, I am trying to use the inbuilt $this->input->post('some_data') function, however $this->input->post() is an empty array.
A print_r($_POST) gives all the variables fully and correctly?
According to the codeigniter docs, The input class "is initialized automatically by the system so there is no need to do it manually", leaving me wondering what else I'm meant to do.
Any thoughts on how I should try to get this working?
Thanks!
You can check, if your view looks something like this (correct):
<form method="post" action="/controller/submit/">
vs (doesn't work):
<form method="post" action="/controller/submit">
Second one here is incorrect, because it redirects without carrying over post variables.
Explanation:
When url doesn't have slash in the end, it means that this points to a file.
Now, when web server looks up the file, it sees, that this is really a directory and sends a redirect to the browser with a slash in the end.
Browser makes new query to the new URL with slash, but doesn't post the form contents. That's where the form contents are lost.
To use $this->input->post() initialize the form helper. You could do that by default in config.
Try This, change
$config['base_url'] = 'http://mywebsite.cl/';
to
$config['base_url'] = 'http://www.mywebsite.cl/';
Some provider auto add www , https , etc to your url, most of the times, this causes this issue
Upgrading from 2.2.x to 3.0.x -- reason post method fails.
If you are upgrading CI 3.x, need to keep the index_page in config file.
Also check the .htaccess file for mod_rewrite. CI_3.x
$config['index_page'] = ''; // ci 2.x
$config['index_page'] = 'index.php'; // ci 3.x
My .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
There's a few things you can look for help solve this.
Has anything changed or been extended in the core CodeIgniter files. Check that system/core/Input.php is an original copy and the contents of application/library and application/core for additional files
Do the other input methods work? What is the result of this when run beside your print_r call?
echo $this->input->user_agent();
What data is output from print_r? Look in application/config/config.php for the line $config['global_xss_filtering']. Is this set to TRUE or FALSE? If TRUE maybe the cross site scripting filter has a problem with the data you're posting (this is unlikely I think)
Use
var_dump($this->input->post());
Please see the screen shot.
Here I am priniting all the post array value
Change your form action,
From this:
<form method="post" action="<?=base_url()?>yourprocess">
Into this:
<form method="post" action="<?=base_url()?>index.php/yourprocess">
Basically, you just need to add "index.php" after your base_url():
action="http://www.yourdomain.com/index.php/yourprocess"
My issue was with an ajax call. I was using this:
type: 'posts',
instead of
type: 'post',
Syntax, D'oh!
I had a similar problem. In my case, I was POSTing to the CodeIgniter website via an outdated Python script and that script was using "http" URLs instead of "https". I had updated the website to use only https a while ago, but this script wasn't updated.
In short, using HTTP caused $this->input->post() to be an empty array.
You are missing the parent constructor.
When your controller is loaded you must
Call the parent CI_Controller class constructor in your controller constructor
The problem is that $this->input->post() does not support getting all POST data, only specific data, for example $this->input->post('my_post_var'). This is why var_dump($this->input->post()); is empty.
A few solutions, stick to $_POST for retrieving all POST data, or assign variables from each POST item that you need, for example:
// variables will be false if not post data exists
$var_1 = $this->input->post('my_post_var_1');
$var_2 = $this->input->post('my_post_var_2');
$var_3 = $this->input->post('my_post_var_3');
However, since the above code is not very DRY, I like to do the following:
if(!empty($_POST))
{
// get all post data in one nice array
foreach ($_POST as $key => $value)
{
$insert[$key] = $value;
}
}
else
{
// user hasen't submitted anything yet!
}
Finally got the issue resolved today. The issue was with the .htaccess file.
Learning to myself: MUST READ THE CODEIGNITER DOCUMENTATION more thoroughly.
To see all the post value try var_dump($this->input->post(ALL));
Solved as follows:
Does the following exclude the redirection that is .htacess that will run the $ this-> input- > post ('name' ) on the controller, in my case it worked perfectly .
abs,
José Camargo]
For my current project in codeignitor I needed to make user profile like this
http://domain.com/userid
Then I tried to add this in router.php
$route['(:any)'] = 'profile/user/$1';
Which is working fine. Now I want to make another URL for language like this
http://domain.com/es
http://domain.com/fr
As for both url uri segments are first, when I type
http://domain.com/es
I see the page of
http://domain.com/userid
I am using .htaccess file for removing index.php in codeignitor. Is there any help how can I achive this task in making shot url for multiple controller. Either with .htaccess or router.php?
Because the routes system works from the top down, if you have multiple rules that can match a url, it picks the first one. So you could do:
$route['(es|fr|en)'] = 'language/$1';
$route['(:any)'] = 'profile/user/$1';
If the first rule matches, it runs, otherwise it tests the profile rule.
You will definitely continue running into issues though with that profile rule, and it would be easier if you did something like:
$route['users/(:any)'] = 'profile/user/$1';
That way it would be more clear what the url is doing, and it will help you for when you are writing rules in the future.
In Zend framework, using the MVC, if A user surf explicitly to http://base/url/index.php instead of just http://base/url, The system thinks the real base url is http://base/url/index.php/ and according to that calculates all the URLs in the system.
So, if I have a controller XXX and action YYY The link will be
http://base/url/index.php/XXX/YYY which is of course wrong.
I am currently solving this by adding a line at index.php:
$_SERVER["REQUEST_URI"]=str_replace('index.php','',$_SERVER["REQUEST_URI"]);
I am wondering if there is a built-in way in ZF to solve this.
You can do it with ZF by using Zend_Controller_Router_Route_Static (phew!), example:
Read the manual page linked above, there are some pretty good examples to be found.
$route = new Zend_Controller_Router_Route_Static(
'index.php',
array('controller' => 'index', 'action' => 'index')
);
$router->addRoute('index', $route);
Can't say I totally disagree with your approach. That said, others may well point out 5000 or so disadvantages. Good luck with that.
Well it really depends on how you want to solve this. As you know the Zend Frameworks build on the front controller pattern, where each request that does not explicitly reference a file in the /public directory is redirected to index.php. So you could basically solve this in a number of ways:
Edit the .htaccess file (or server configuration directive) to rewrite the request to the desired request:
RewriteRule (.*index.php) /error/forbidden?req=$1 // Rewrite to the forbidden action of the error controller.
RewriteRule index.php /index // Rewrite the request to the main controller and action
Add a static route in your bootstrapper as suggested by karim79.
Use mod_rewrite. Something like this should do it:
RewriteRule ^index.php/(.*)$ /$1 [r=301,L]
I don't think you should use a route to do this.
It's kind of a generic problem which shouldn't be solved by this way.
You better should have to do it in your .htaccess, which will offer you a better & easier way to redirect the user to where you want, like to an error page, or to the index.
Here is the documentation page for the mod_rewrite
I've never faced this problem using Zend Framework. just do not link to index.php file. that's it. and when your are giving your application's address to users, just tell them to go to http://base/url/
when the user enters http://base/url/ her request URI is base/url and your .htaccess file routs the request to index.php, but the request IS base/url. you do not need to remove 'index.php' from the request. because it is not there.
when you are trying to generate URLs for links and forms and ..., use the built-in url() view helper to generate your links. like this:
// in some view script
<a href="<?php
echo $this->url( array('controller'=>'targetController','action'=>'targetAction') );
?>" >click</a>
do not worry about the link. Zend will generate a URL for you.
The way I look at this is that if I have a website powered by PHP and a user goes to http://site/index.aspx then I would send a 404.
Even though index.php does exist in theory, it's not a valid URL in my application so I would send a 404 in this case too.