非常に基本的なphpルーティングを行う方法に関するチュートリアルまたは説明を探しています。
たとえば、mywebsite.com/users のようなリンクにアクセスすると、ルート クラスの get メソッドを実行して、laravel と同じ方法でデータを提供したいと考えています。
Route::get('users', function()
{
return 'Users!';
});
誰かがこれを行う方法を説明したり、さらに情報を提供したりできますか?
非常に基本的なphpルーティングを行う方法に関するチュートリアルまたは説明を探しています。
たとえば、mywebsite.com/users のようなリンクにアクセスすると、ルート クラスの get メソッドを実行して、laravel と同じ方法でデータを提供したいと考えています。
Route::get('users', function()
{
return 'Users!';
});
誰かがこれを行う方法を説明したり、さらに情報を提供したりできますか?
最も一般的な構成では、PHP は Web サーバーに依存してルーティングを行います。これは、リクエスト パスをファイルにマッピングすることによって行われます。www.example.org/test.php をリクエストすると、Web サーバーは実際に定義済みのディレクトリで test.php という名前のファイルを探します。
この目的に役立つ機能があります。多くの Web サーバーでは、www.example.org/test.php/hello を呼び出すこともできますが、それでもtest.php は実行されます。PHP は、$_SERVER['PATH_INFO']
変数を介してアクセス可能な、要求されたパス内の追加のものを作成します。この場合、「/hello」が含まれます。
これを使用して、次のような非常に単純なルーターを構築できます。
<?php
// First, let's define our list of routes.
// We could put this in a different file and include it in order to separate
// logic and configuration.
$routes = array(
'/' => 'Welcome! This is the main page.',
'/hello' => 'Hello, World!',
'/users' => 'Users!'
);
// This is our router.
function router($routes)
{
// Iterate through a given list of routes.
foreach ($routes as $path => $content) {
if ($path == $_SERVER['PATH_INFO']) {
// If the path matches, display its contents and stop the router.
echo $content;
return;
}
}
// This can only be reached if none of the routes matched the path.
echo 'Sorry! Page not found';
}
// Execute the router with our list of routes.
router($routes);
?>
簡単にするために、ルーターをクラスにしませんでした。しかし、これからはそれも問題にならないはずです。
このファイルに index.php という名前を付けたとしましょう。これで www.example.org/index.php/hello を呼び出して、素敵な "Hello, World!" を取得できます。メッセージ。または www.example.org/index.php/ でメイン ページを取得します。
その URL の「index.php」はまだ醜いですが、URL 書き換えを使用してこれを修正できます。Apache HTTPD.htaccess
では、次の内容のファイルを同じディレクトリに配置します。
RewriteEngine on
RewriteRule ^(.*)$ index.php/$1
そして、そこにいます!10 行以下のロジック コード (コメントとルート リストを除く) を使用した独自のルーター。
ええと...インターネットにはphpルーティング用のフレームワークがたくさんあります。必要に応じて、 https://packagist.org/search/?q=routeから試すことができます。しかし、正直なところ、手続き型の PHP の世界から来た場合、初めて問題を起こす可能性があります。
必要に応じて、私自身のプロジェクトで使用した Jesse Boyer によって記述された次のコードを使用できます。アプリケーション構造は次のとおりです-
【アプリケーションフォルダ】
<?php
/**
* @author Jesse Boyer <contact@jream.com>
* @copyright Copyright (C), 2011-12 Jesse Boyer
* @license GNU General Public License 3 (http://www.gnu.org/licenses/)
* Refer to the LICENSE file distributed within the package.
*
* @link http://jream.com
*
* @internal Inspired by Klein @ https://github.com/chriso/klein.php
*/
class Route
{
/**
* @var array $_listUri List of URI's to match against
*/
private static $_listUri = array();
/**
* @var array $_listCall List of closures to call
*/
private static $_listCall = array();
/**
* @var string $_trim Class-wide items to clean
*/
private static $_trim = '/\^$';
/**
* add - Adds a URI and Function to the two lists
*
* @param string $uri A path such as about/system
* @param object $function An anonymous function
*/
static public function add($uri, $function)
{
$uri = trim($uri, self::$_trim);
self::$_listUri[] = $uri;
self::$_listCall[] = $function;
}
/**
* submit - Looks for a match for the URI and runs the related function
*/
static public function submit()
{
$uri = isset($_REQUEST['uri']) ? $_REQUEST['uri'] : '/';
$uri = trim($uri, self::$_trim);
$replacementValues = array();
/**
* List through the stored URI's
*/
foreach (self::$_listUri as $listKey => $listUri)
{
/**
* See if there is a match
*/
if (preg_match("#^$listUri$#", $uri))
{
/**
* Replace the values
*/
$realUri = explode('/', $uri);
$fakeUri = explode('/', $listUri);
/**
* Gather the .+ values with the real values in the URI
*/
foreach ($fakeUri as $key => $value)
{
if ($value == '.+')
{
$replacementValues[] = $realUri[$key];
}
}
/**
* Pass an array for arguments
*/
call_user_func_array(self::$_listCall[$listKey], $replacementValues);
}
}
}
}
この 2 行目では、'/php/cfc/' の代わりに、'Application-folder' などの localhost プロジェクト ディレクトリ名を入力する必要があります。
RewriteEngine On
RewriteBase /php/cfc/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]
index.php ファイルでは、次のコードを記述する必要があります。
<?php
include "route.php";
/**
* -----------------------------------------------
* PHP Route Things
* -----------------------------------------------
*/
//define your route. This is main page route. for example www.example.com
Route::add('/', function(){
//define which page you want to display while user hit main page.
include('myindex.php');
});
// route for www.example.com/join
Route::add('/join', function(){
include('join.php');
});
Route::add('/login', function(){
include('login.php');
});
Route::add('/forget', function(){
include('forget.php');
});
Route::add('/logout', function(){
include('logout.php');
});
//method for execution routes
Route::submit();
これは私にとって完璧に機能します。それがあなたにとってもうまくいくことを願っています...
ハッピーコーディング... :)
まあ、それを使用してネイティブ PHP で PHP ルーティング システムを作成することは可能です。
これは、php ルーティング システムを数分でセットアップする方法を示しています: https://www.youtube.com/watch?v=ysnW2mRbZjE