简体   繁体   中英

vanity profile urls in php mvc not working

I am making a social networking site where I have created my own PHP mvc but I am having problems in url rewriting. My mvc works this way.

If this is the domain www.example.com/manage/posts/11111 , manage is the class, post is the method in that class & 1111 is a parameter.

The problem is that I cannot create vanity profile urls since they will not work. I want each user to have vanity profile url ie www.example.com/username but this will search for a class named username .

Kindly advise me on how

a) I can achieve vanity profile urls such as www.example.com/username without adding anything such as www.example.com/users/username .

I know there a other PHP MVCs but I just want to use my own

This is my current htaccess code::

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/?$  index.php?ref_url=$1 [NC,L,QSA]
</IfModule>

I don't know why all people try to build their own frameworks there are so many really good frameworks that have a look at security and many more. But ok i think your problem is your rewrite rule.

RewriteRule ^(.*)$  index.php?ref_url=$1 [NC,L,QSA]

Normally you should rewrite all your input to a given file and parse the url and call the controller you need. To get a better understanding take a look at the Symfony2 Routing component.

http://symfony.com/doc/current/components/routing/introduction.html

You should first create a router to route your urls to controllers/classes. I will prefer klein router or FastRoute for its simplicity.

After placing a router do what u want:

(Example)

$router=new Router;

$router->addroute("/[:username]",
   function(){ 
       //Bring UserAccount Details from DB using [:username]
   }
);

$router->addroute("/manage/posts/[:id]",
   function(){
       //Get Post details using [:id]
   }

$router->dispatch();

**NB:**This is only a basic representation of usage of a router

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM