Stefan Koenen
Stefan Koenen

Reputation: 2337

PHP OOP routing

i want to make a very simple website but with OOP PHP. I got enough experience in programming (c#, c++, php, js and more) so i know how to make classes etc, but the thing i dont understand with php is the correct way to call things.

there are hundreds of tutorials oop php on the internet but nothing with this (or maybe its a weird question :P). let me explain.

for example i want a news website and i got a class News with the function create. if i follow the url mywebsite.com/news/create or mywebsite.com/news?action=create i want to execute the php class News, action create. but how am i suppose to do this. do i need to make in index.php

if(action == news) news->create();

and for every action another... i dont think so :P. so how can i make this correctly? or is it better to take a simple mvc framework?

Thnx, Stefan.

Upvotes: 0

Views: 1779

Answers (2)

NaturalBornCamper
NaturalBornCamper

Reputation: 3866

I would use the CodeIgniter framework for this, it is EXTREMELY easy to install, plus it uses the the MVC design pattern.

Then to make your url like this: "mywebsite.com/news/create" you can change a simple thing in the htaccess file like such:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Source: http://codeigniter.com/user_guide/general/urls.html


If on the other hand you don't want to use a framework, you can just use Apache's mod_rewrite to remove the script filename, then using php's explode function to get the function and parameters from the $_SERVER["REQUEST_URI"] variable.

There is a good example here: http://www.phpaddiction.com/tags/axial/url-routing-with-php-part-one/

Upvotes: 2

Stefan Koenen
Stefan Koenen

Reputation: 2337

i know many frameworks and i worked with codeigniter, and yii. I prefer yii but the mean question for me is, is it possible to work without such frameworks and route things or is it better to take a framework.

Upvotes: 0

Related Questions