Clean URLs in MVC

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Indeed it will :) Hopefully I have some time this week to get started with something.

I haven't looked at RoR yet but your idea sounds cool. My main purpose is to be able to set the routes from within the app. So I won't have to add another - seperate - htaccess layer on top to get clean url's. That would also be a maintenance nightmare, as with each new route I would have to change the htaccess.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

matthijs wrote:My main purpose is to be able to set the routes from within the app. So I won't have to add another - seperate - htaccess layer on top to get clean url's. That would also be a maintenance nightmare, as with each new route I would have to change the htaccess.
You can do that with the class provided in the code I linked to. I think the class is Http_PathInfo and there is code in "examples/pathinfo". You can give it custom mappings for different types of URLs so you only need one very simple rule in .htaccess (an example is provided). Not only that -- I made it so you can restart the mapping in the dispatched controller -- so you can provide basic mappings in the Front Controller and then append additional mappings in a dispatched Action Controller and have the mapper continue mapping the path where it left off. I have found that it greatly reduces the size of the FC map on sites that do lots of fancy paths. I essentially distributes the maps and lazy loads them with the corresponding Action Controller.
(#10850)
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

That's very very cool.

The skeleton seems quite stable and mature already. And still developing. Do you still work together with the other developers in the current development?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

matthijs wrote:The skeleton seems quite stable and mature already. And still developing. Do you still work together with the other developers in the current development?
I still get code here an there from people and mold it in. I get ideas from the various frameworks around and simplify them by removing the bells, whistles and PHP5 stuff.

I have never really treated it as a framework of the site and logo variety. It has always been minimal and controller-centric to show the basic ideas. I have thought of adding so other common code to it -- you know, a simple DB wrapper around PDO, a minimal ActiveRecord implementation, basic Access Control (with the proverbial sign-in page example), maybe Phemto, stuff like that. But it takes time to boil complex things down to something simple -- so it goes slow. I am always open to comments and contributions.
(#10850)
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Re: Clean URLs in MVC

Post by MrPotatoes »

arborint wrote:
matthijs wrote:For a project I'm using Arborint's (simplified) frontcontroller class (seen here). Using this class, one gets urls like
index.php?action=page
...
So that will make thing even more complicated. Any hints for directions I should look into?
Since I was mentioned -- you can get the "less simplified" version here that has an example using a class to map PATH_INFO values onto the Request. I think there is a .htaccess file included with the example. You an even do Rails style routes by providing your own map. And because the mapping is done on the Request object -- not inside the Front Controller -- you can mix both style URLs.
that sounds completely badass. i'll need help even understanding this but i think this is awesome and i have been wanting to put this into my framework and hopefully i can do it for my proof-of-concept.

i like the idea of mixing both (if i get lazy) so that's a nice feature. i'm totally gonna check this out sometime soon :thumbup:
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Clean URLs in MVC

Post by Christopher »

MrPotatoes wrote:that sounds completely badass. i'll need help even understanding this but i think this is awesome and i have been wanting to put this into my framework and hopefully i can do it for my proof-of-concept.

i like the idea of mixing both (if i get lazy) so that's a nice feature. i'm totally gonna check this out sometime soon :thumbup:
Feel free to borrow, steal, mutilate, and improve however you heat desires. :) That's what that code is for. And if you come up with something cool post it and I wll borrow it back. ;)
(#10850)
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post by MrPotatoes »

will do. if i can figure it out.

this is something that i've actually wanted to do but i couldn't do because i didn't know where to start or how to do it. but if i add anything to it that makes it better or cooler or whatever i'l definatly post it. this is your code?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Yep, if you have questions about the code or design issues relating to controllers feel free to ask.
(#10850)
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post by MrPotatoes »

i definatly will. that is some pretty sick code. that is exactly what i needed. i already have a framework so the routing is what i want to play with. thank you for writing this code. it's perfect, shoot, i think it'll be great for SEO :D :D :D

once i figure out the forums i might just come over and try this :)
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post by MrPotatoes »

did you ever figure this out? i'm deperatly trying to figure this out now and it's really gettin' to me

i'm trying some of the code from the skeleton but i can't seem to get it to work at all and i'm not even sure i know what's going on because what i need this might not be able to do

are there any more working tutorials on this?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Can you get the example in "examples/pathinfo/" to work? If not can you tell me what is not working?
(#10850)
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post by MrPotatoes »

yeah i can get it to work. as a matter of fact i made my own little one. i'm not sure how safe or even efficent it is right now but this is my code thusfar:

Code: Select all

echo '<a href="'. $_SERVER["SCRIPT_NAME"] . '/thing/2006/January/1st/param1/param2/param3/">date link</a><BR>';
	echo '<a href="'. $_SERVER["SCRIPT_NAME"] . '/forums/off_topic/topic+name+is+this/">link to forums</a><BR>';

	$url = $_SERVER['PATH_INFO'];

	// remove all the slashes-------|
	if (substr($url, 0, 1) == '/') $url = substr($url, 1);
	if (substr($url, -1) == '/') $url = substr($url, 0, -1);
	if ($url == '/') $url = '';
	$url = explode('/', $url);
	//------------------------------|

	for ($j = 0; $j < count($url); $j++)
	  	echo $j . ' ' . $url[$j] . '<BR>';

	if ($url[0] == 'thing')
		echo 'omg you got it';
	else
		echo 'you suck at life taters.  go outside and give up';
i think i can get this to work very easilly but i'd still like to use a map kind of a deal because i know my framework would get huge and it's easier to work with it instead of for it.

this one is also very very light. i don't know where any problems would arise with this. yet at least ;)

i'm looking thru your code right now and seeing what i can take from that and add tohere and then i'll send it back your way and you can use it if you want. i'm just making mine so simple.

also, i can add query strings to the end of it but i can't put the prettyURLs at the end of a following querystring ('/query_string/thinger/<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>/?shizzy=thing/more/query/string/">
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

MrPotatoes wrote:also, i can add query strings to the end of it but i can't put the prettyURLs at the end of a following querystring ('/query_string/thinger/<span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span>/?shizzy=thing/more/query/string/">
That is correct.
(#10850)
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post by MrPotatoes »

lol, i purposly went thru that thing to make sure that it didn't have cuss words in it and yet i still missed that one. well, what can i say? that's how i alpha/debug my code lol

[edit] apparently i can't spell worth a damn tho *sad face*
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Two things:

First, why are clean URLs better?

Second, I once ran across a website, coded in PHP, with clean URLs, but he did not use mod_wewrite. What he did was throw everything into a database and use a script he wrote to save everything into static files every time it was changed. It's not practical for anything more than a very small and sparsely updated website, but it is one way to do it..

P.S. I didn't read this entire thread so if someonen already figured it out, kudos.
Post Reply