[英]Dynamic php subpages
我想知道如何通過php創建子頁面。 我知道有一種使用GET參數的方法,例如:
example.com/index.php?category=1
我對例如instagram.com上發現的某些功能更感興趣:
instagram.com/example
以下示例是如何生成的? 該系統如何工作?
我想要一個簡單的頁面,根據破折號后的標識符顯示內容。 另外,他們如何在每個專業網站上刪除.php擴展名?
提前致謝
這通常是使用MVC框架(例如laravel,codeigniter等)完成的。有許多可用的方法來實現您所尋找的東西。
http://www.codeproject.com/Articles/826642/Why-to-use-Framework-in-PHP列出了其中的一些。
使用MVC有很多優點,包括采用良好的頁面結構,並可能為您提供預構建包中正在尋找的功能。
我建議對諸如laravel之類的產品進行一些研究,看看您的情況如何。
您也可以更改htaccess文件中其他說明的apache配置。
您正在尋找的是URL REWRITING
。 根據所使用的HTTP server
,有幾種方法可以完成此操作。
最常用的HTTP服務器是Apache。
創建一個包含以下內容的php文件:
<?php
phpinfo();
?>
使用瀏覽器打開頁面,您應該能夠看到正在運行的HTTP服務器。 搜索SERVER_SOFTWARE
,其中必須說類似Apache
, Nginx
或LightHTTP
。
如果服務器使用的是apache,則應搜索apache php .htaccess url rewriting
否則,您可以搜索[server software] php url rewriting
或[server software] php pretty urls
互聯網上有成千上萬的人問過同樣的問題,所以我認為您也許可以從這里幫助自己。 祝好運!
這是由稱為URL路由的技術完成的,有幾種方法。弄清楚instagram是如何做到這一點並不容易。
有一個很好的非面向對象的例子:
http://blogs.shephertz.com/2014/05/21/how-to-implement-url-routing-in-php/
大多數PHP框架(Laravel等)也提供了未來。
就我個人而言,我現在使用一個名為AltoRouter的php包https://github.com/dannyvankooten/AltoRouter
我想還有很多其他方法。
使用ALTO ROUTER:
基本邏輯是,您正在將url和具有其方法(post,get)的“對象”映射到哪個對象,哪個控制器將處理該對象以及什么是控制器方法。
$router->map('GET','/example', 'Controllers\ExampleController@getShowExamplePage' ,'example' );
還有一個帶有getShowExamplePage()方法的ExampleController類
public function getShowExamplePage(){
include(__DIR__ . "/../../views/example.php");
並在您的index.php文件中
您檢查用戶輸入的網址是否在您映射的$ router對象中?
$match = $router->match();//it returns true or false
if(!match)
{
//--
u can redirect a error 404 PAGE
//---
}
else
{
//Example the use entered url www.example.com/example
list($controller,$method) = explode("@",$match['target']);//To get what is the controller and its method.
//If that method of the contoller avaliable run that method
if(is_callable(array($controller,$method))){
$object = new $controller();
call_user_func_array(array($object ,$method) , array($match['params']));
}else {
echo "Cannot find $controller-> $method";
exit();
}
}
您只需利用“面向對象編程”的優勢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.