This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
turn URL route into funciton arguments php mvc
CMS Routing in MVC
I'm currently trying to rewrite a PHP router.
The new htaccess rewrite has the follows.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/index.php?url=$1 [L]
</IfModule>
Whilst in index.php in public, I am getting the URL using the $url = $_GET['url'];
What I need to do is to pass $url to the Router function:: route($url)
If a URL is passed as : /page/function/$params which would then translate as : index.php?url=page/xapp/function, I'd need to map and route to Controller xapp and call function($params).
By this time, the autoloader has already been called. I'd also need to set a default function to be called if only /page/ is called.
How would I achieve this in a router?
You should check out the code of klein.php, a small php router.
I think you should figure it from that solution.
If not, check out also slim here
Related
This question already has answers here:
.htaccess rewrite GET variables
(7 answers)
Closed 4 years ago.
I have a website with a dynamic structure: for every $_GET['action'] I include a file with a different content, while the footer and the header always remain the same. example: www.mysite.com/index.php?action=service1
I would like to change this "structure" in a way:
www.mysite.com/service1/ is there a way to include the $_GET['action'] request in every folder I create?
The site in question has over 100+ $_GET['action'] and creating a page for each action would become heavy .
any advice on how to do?
You can set up your .htaccess to pipe certain values to your $_GET variables.
Learn more here.
The basic idea is to add something like:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^service/(\d+).*$ ./myActionscript.php?id=$1 [L]
Then all requests you send to /service/{number} will go to your file myActionScript and you can do what you want with that {number} there by using string manipulation on the $_SERVER['REQUEST_URI'] to get it.
This question already has answers here:
CodeIgniter removing index.php from url
(35 answers)
Closed 5 years ago.
My link show this:
http://localhost/project/index.php
I want this:
http://localhost/project/
Do not display extension
You don't understand MVC pattern, there is no direct access to files within URL, everything is redirected to one page.
Full URL: http://localhost/project/home/about
root: http://localhost/project
controller: home
method inside given controller: about
Read more about MVC in Codeigniter's website: https://www.codeigniter.com/user_guide/overview/mvc.html
Or here: https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
CodeIgniter provide Routing rules/URI Routing for this.
You can achive this by adding variable to $route array in
/application/config/routes.php file
$old_path = 'home/about'; // no need to add .php to about
$new_path='home/about'; //or about or any other name you want
$route[$new_path] =$old_path;
Now you can visit page by only
http://localhost/project/home/about
For more details :-
https://www.codeigniter.com/user_guide/general/routing.html?highlight=route
Codeigniter's mvc works as project_folder/index.php/controller/function.
If you modify this behaviour with htaccess it might collapse the behaviour of the framework.
Learn how it works and get clarified. This link might help you.
Create a file with the name ".htaccess" just ".htaccess" NOT "test.htaccess" or so.
Write this code in it ->
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Now you can link your sites like this -> Test
This question already has answers here:
How to create friendly URL in php?
(8 answers)
Closed 6 years ago.
I couldn't find much help while I was looking for my answer that's the reason for the question on here.
Basically I have this bit of code :
More Info</span>
now how is it possible to get what I am passing ?
Do I make a php file called manage.php ? if so what makes it so that it looks at manage.php ? and then get it through $_GET but how is it possible ?
I am not sure how to go about it so I would appreciate any of yours suggestions.
Thanks
Your problem can have two possible solutions:
By creating manage.php file and passing query string to it.
Your code for the link will like this:
More Info</span>
Now on manage.php page, you can access user_id like this:
<?php $user_id = $_GET["user_id"]; ?>
Using .htaccess, create .htaccess file and put the code below in it. .htaccess file must be placed in same dir where your index.php file is.
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?/$1 [L] </IfModule>
Now you can access your query strings on index.php file using $_SERVER['REQUEST_URI'] global variable.
More Info</span>
Your link to access page must look like above note the / after 10000 else user_id will get merge with 10000
Chatting session was performed for the question, view chat transcript here
You need a routing-engine to manage that type of url.
There are many framework that include routing. The most used is Symfony http://symfony.com/doc/current/routing.html
The general syntax would be as follows:
http://domain_name.com/script.php?parameter1=value¶meter2=value
In your case you would have the following assuming that your parameter is
user_id:
More Info</span>
In PHP you would use $_GET['user_id'] to fetch the passed value
$userID = $_GET['user_id'];
By including these lines in .htaccess
RewriteEngine On
RewriteRule ^manage\/([0-9]+)$ manage.php?user_id=$1
You could reach the same page with the following syntax:
http://domainname/manage/2313
If you mean to say that you need to route to a different php file manage.php and get the passed variable from the previous page then either you can modify the link to
"manage.php?val=10000".$user["user_id"]
and then use $_GET to get the passed variable
Or else you can keep the same url use .htaccess to route your request to manage.php?val=$1
or else in frameworks like codeigniter you have routes file where you can define your route.
Is there any way I can have good looking URL in PHP? any default php URL would look like this: http://example.com/something/?post=something But Is It possible to have It like this: http://example.com/something/user Is It possible to remove ?post= without using .htaccess
Here is some example code that I have been working on, Which on click of a post It would access my database and load the content:
<?php
if(!isset($_GET['post'])) {
$q = mysql_query("SELECT * FROM posts WHERE postID='something'");
} else {
$id = $_GET['post'];
$id = mysql_real_escape_string($id);
$q = mysql_query("SELECT * FROM posts WHERE postID='$id'");
}
$p = mysql_fetch_object($q);
?>
Thank you for your Time!
To get clean URLs you'll have to use mod_rewrite module, but you can minimize it's use, if you leave url parsing to your own script and have only one entry point. Look how it's made in WordPress:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
So if there's no real file or directory that is requested in URL, each and every request is redirected to index.php and then parsed by some internal route mapping script.
There's an example of such script in similar question.
I suggest for having clean URL use one of php frameworks which default have this ability.
if you want to use pure php you should use a Router in your project which means you should write your own php framework.
have you ever worked with any php framework??
I suggest using laravel or cakephp for entry point of learning php frameworks.
You need to define a Router and Dispatcher in your project.The Router extracts url and dispatcher calls that function related to url.in the other word,you should impelement frontcontroller design pattern in your project.I suggest check this tutorial
http://www.sitepoint.com/front-controller-pattern-1/
What's the best way to implement a URL interpreter / dispatcher, such as found in Django and RoR, in PHP?
It should be able to interpret a query string as follows:
/users/show/4 maps to
area = Users
action = show
Id = 4
/contents/list/20/10 maps to
area = Contents
action = list
Start = 20
Count = 10
/toggle/projects/10/active maps to
action = toggle
area = Projects
id = 10
field = active
Where the query string can be a specified GET / POST variable, or a string passed to the interpreter.
Edit: I'd prefer an implementation that does not use mod_rewrite.
Edit: This question is not about clean urls, but about interpreting a URL. Drupal uses mod_rewrite to redirect requests such as http://host/node/5 to http://host/?q=node/5. It then interprets the value of $_REQUEST['q']. I'm interested in the interpreting part.
If appropriate, you can use one that already exists in an MVC framework.
Check out projects such as -- in no particular order -- Zend Framework, CakePHP, Symfony, Code Ignitor, Kohana, Solar and Akelos.
have a look at the cakephp implementation as an example:
https://trac.cakephp.org/browser/trunk/cake/1.2.x.x/cake/dispatcher.php
https://trac.cakephp.org/browser/trunk/cake/1.2.x.x/cake/libs/router.php
You could also do something with mod_rewrite:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ $1 [L]
RewriteRule ^([a-z]{2})/(.*)$ $2?lang=$1 [QSA,L]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
This would catch urls like /en/foo /de/foo and pass them to index.php with GET parameters 'lang' amd 'url'. Something similar can be done for 'projects', 'actions' etc
Why specifically would you prefer not to use mod_rewrite? RoR uses mod_rewrite. I'm not sure how Django does this, but mod_php defaults to mapping URLs to files, so unless you create a system that writes a separate PHPfile for every possible URL (a maintenance nightmare), you'll need to use mod_rewrite for clean URLs.
What you are describing in your question should actually be the URL mapper part. For that, you could use a PEAR package called Net_URL_Mapper. For some information on how to use that class, have a look at this unit test.
The way that I do this is very simple.
I use wordpress' .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
What this .htaccess does is when something returns a 404, it sends the user to index.php.
In the above, /index.php is the "interpreter" for the URL.
In index.php, I have something along the lines of:
$req = $_SERVER['REQUEST_URI'];
$req = explode("/",$req);
The second line splits up the URL into sections based on "/". You can have
$area = $req['0'];
$action= $req['1'];
$id = $req['2'];
What I end up doing is:
function get_page($offset) {//offset is the chunk of URL we want to look at
$req = $_SERVER['REQUEST_URI'];
$req = explode("/",$req);
$page = $req[$offset];
return $page;
}
$area = get_page(0);
$action = get_page(1);
$id = get_page(2);
Hope this helps!
Just to second #Cheekysoft's suggestion, check out the Zend_Controller component of the Zend Framework. It is an implementation of the Front Controller pattern that can be used independently of the rest of the framework (assuming you would rather not use a complete MVC framework).
And obviously, CakePHP is the most similar to the RoR style.
I'm doing a PHP framework that does just what you are describing - taking what Django is doing, and bringing it to PHP, and here's how I'm solving this at the moment:
To get the nice and clean RESTful URLs that Django have (http://example.com/do/this/and/that/), you are unfortunately required to have mod_rewrite. But everything isn't as glum as it would seem, because you can achieve almost the same thing with a URI that contains the script's filename (http://example.com/index.php/do/this/and/that/). My mod_rewrite just forwards all calls to that format, so it's almost as usable as without the mod_rewrite trick.
To be truthful, I'm currently doing the latter method by GET (http://example.com/index.php?do/this/and/that/), and fixing stuff in case there are some genuine GET variables passed around. But my initial research says that using the direct slash after the filename should be even easier. You can dig it out with a certain $_SERVER superglobal index and doesn't require any Apache configuration. Can't remember the exact index off-hand, but you can trivially do a phpinfo() testpage to see how stuff look like under the hood.
Hope this helps.