国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目次
ルーティングの概要
接続ルート
出力
渡された引數(shù)
アクションメソッドの引數(shù)として
數(shù)値的にインデックス付けされた配列として
ルーティングアレイの使用
URL の生成
Output
Redirect Routing
Example
Output for URL 2

CakePHP ルーティング

Sep 10, 2024 pm 05:25 PM
php cakephp PHP framework

この章では、ルーティングに関連する次のトピックについて學(xué)習(xí)します -

  • ルーティングの概要
  • 接続ルート
  • 引數(shù)をルートに渡す
  • URL を生成しています
  • リダイレクト URL

ルーティングの概要

このセクションでは、ルートを?qū)g裝する方法、URL からコントローラーのアクションに引數(shù)を渡す方法、URL を生成する方法、特定の URL にリダイレクトする方法について説明します。通常、ルートはファイル config/routes.php に実裝されます。ルーティングは 2 つの方法で実裝できます -

  • 靜的メソッド
  • スコープ付きルートビルダー

ここでは、両方のタイプを示す例を示します。

// Using the scoped route builder.
Router::scope('/', function ($routes) {
   $routes->connect('/', ['controller' => 'Articles', 'action' => 'index']);
});
// Using the static method.
Router::connect('/', ['controller' => 'Articles', 'action' => 'index']);

どちらのメソッドも ArticlesController のインデックス メソッドを?qū)g行します。 2 つの方法のうち、スコープ ルート ビルダー の方がパフォーマンスが優(yōu)れています。

接続ルート

Router::connect() メソッドはルートの接続に使用されます。以下はメソッドの構(gòu)文です -

static Cake\Routing\Router::connect($route, $defaults =[], $options =[])

Router::connect() メソッドには 3 つの引數(shù)があります -

  • 最初の引數(shù)は、照合する URL テンプレートです。

  • 2 番目の引數(shù)には、ルート要素のデフォルト値が含まれます。

  • 3 番目の引數(shù)にはルートのオプションが含まれており、通常は正規(guī)表現(xiàn)ルールが含まれます。

これはルートの基本的な形式です -

$routes->connect(
   'URL template',
   ['default' => 'defaultValue'],
   ['option' => 'matchingRegex']
);

以下に示すように、config/routes.php ファイルを変更します。

config/routes.php

<?php
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
$routes->setRouteClass(DashedRoute::class);
$routes->scope('/', function (RouteBuilder $builder) {
   // Register scoped middleware for in scopes.
      $builder->registerMiddleware('csrf', new CsrfProtectionMiddleware([
      'httpOnly' => true,
   ]));
   $builder->applyMiddleware('csrf');
   $builder->connect('/', ['controller' => 'Tests', 'action' => 'show']);
   $builder->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
   $builder->fallbacks();
});

src/Controller/TestsController.php に TestsController.php ファイルを作成します。 コントローラー ファイルに次のコードをコピーします。

src/Controller/TestsController.php

<?php
declare(strict_types=1);
namespace App\Controller;
use Cake\Core\Configure;
use Cake\Http\Exception\ForbiddenException;
use Cake\Http\Exception\NotFoundException;
use Cake\Http\Response;
use Cake\View\Exception\MissingTemplateException;
class TestsController extends AppController {
   public function show()
   {
   }
}

src/Template の下にフォルダー Tests を作成し、そのフォルダーの下に show.php という名前の ビュー ファイル を作成します。そのファイルに次のコードをコピーします。

src/Template/Tests/show.php

<h1>This is CakePHP tutorial and this is an example of connecting routes.</h1>

http://localhost/cakephp4/ にある次の URL にアクセスして、上記の例を?qū)g行します

出力

上記の URL は次の出力を生成します。

Above URL

渡された引數(shù)

渡された引數(shù)は、URL で渡される引數(shù)です。これらの引數(shù)はコントローラーのアクションに渡すことができます。これらの渡された引數(shù)は、3 つの方法でコントローラーに與えられます。

アクションメソッドの引數(shù)として

次の例は、コントローラーのアクションに引數(shù)を渡す方法を示しています。次の URL (http://localhost/cakephp4/tests/value1/value2

) にアクセスしてください。

これは次のルート行と一致します。

$builder->connect('tests/:arg1/:arg2', ['controller' => 'Tests', 'action' => 'show'],['pass' => ['arg1', 'arg2']]);

ここでは、URL の value1 が arg1 に割り當(dāng)てられ、value2 が arg2 に割り當(dāng)てられます。

數(shù)値的にインデックス付けされた配列として

引數(shù)がコントローラーのアクションに渡されたら、次のステートメントで引數(shù)を取得できます。

$args = $this->request->params[‘pass’]

コントローラーのアクションに渡される引數(shù)は $args 変數(shù)に格納されます。

ルーティングアレイの使用

次のステートメントによって引數(shù)をアクションに渡すこともできます -

$routes->connect('/', ['controller' => 'Tests', 'action' => 'show',5,6]);

上記のステートメントは、2 つの引數(shù) 5 と 6 を TestController の show() メソッドに渡します。

次のプログラムに示すように、config/routes.php ファイルを変更します。

config/routes.php

<?php
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
$routes->setRouteClass(DashedRoute::class);
$routes->scope('/', function (RouteBuilder $builder) {
// Register scoped middleware for in scopes.
$builder->registerMiddleware('csrf', new CsrfProtectionMiddleware([
      'httpOnly' => true,
   ]));
   $builder->applyMiddleware('csrf');
   $builder->connect('tests/:arg1/:arg2', ['controller' => 'Tests', 'action' => 'show'],['pass' => ['arg1', 'arg2']]);
   $builder->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
   $builder->fallbacks();
});

src/Controller/TestsController.php に TestsController.php ファイルを作成します。 コントローラー ファイルに次のコードをコピーします。

src/Controller/TestsController.php

<?php
declare(strict_types=1);
namespace App\Controller;
use Cake\Core\Configure;
use Cake\Http\Exception\ForbiddenException;
use Cake\Http\Exception\NotFoundException;
use Cake\Http\Response;
use Cake\View\Exception\MissingTemplateException;
class TestsController extends AppController {
public function show($arg1, $arg2) {
      $this->set('argument1',$arg1);
      $this->set('argument2',$arg2);
   }
}

src/Template にフォルダー Tests を作成し、そのフォルダーの下に show.php という名前の View ファイルを作成します。そのファイルに次のコードをコピーします。

src/Template/Tests/show.php.

<h1>This is CakePHP tutorial and this is an example of Passed arguments.</h1>
<?php
   echo "Argument-1:".$argument1."<br/>";
   echo "Argument-2:".$argument2."<br/>";
?>

次の URL http://localhost/cakephp4/tests/Virat/Kunal にアクセスして、上記の例を?qū)g行します

出力

実行すると、上記の URL は次の出力を生成します。

Passed Argument

URL の生成

これは CakePHP の優(yōu)れた機(jī)能です。生成された URL を使用すると、コード全體を変更することなく、アプリケーション內(nèi)の URL の構(gòu)造を簡(jiǎn)単に変更できます。

url( string|array|null $url null , boolean $full false )

上記の関數(shù)は 2 つの引數(shù)を取ります -

  • 最初の引數(shù)は、'controller'、'action'、'plugin' のいずれかを指定する配列です。さらに、ルーティングされた要素またはクエリ文字列パラメーターを提供できます。文字列の場(chǎng)合は、任意の有効な URL 文字列の名前を指定できます。

  • true の場(chǎng)合、完全なベース URL が結(jié)果の先頭に追加されます。デフォルトは false です。

次のプログラムに示すように、config/routes.php ファイルを変更します。

config/routes.php

<?php
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
$routes->setRouteClass(DashedRoute::class);
$routes->scope('/', function (RouteBuilder $builder) {
   // Register scoped middleware for in scopes.
   $builder->registerMiddleware('csrf', new CsrfProtectionMiddleware([
      'httpOnly' => true,
   ]));
   $builder->applyMiddleware('csrf');
   $builder->connect('/generate',['controller'=>'Generates','action'=>'show']);
   $builder->fallbacks();
});

Create a GeneratesController.php file at src/Controller/GeneratesController.php. Copy the following code in the controller file.

src/Controller/GeneratesController.php

<?php
declare(strict_types=1);
namespace App\Controller;
21
use Cake\Core\Configure;
use Cake\Http\Exception\ForbiddenException;
use Cake\Http\Exception\NotFoundException;
use Cake\Http\Response;
use Cake\View\Exception\MissingTemplateException;
class GeneratesController extends AppController {
   public function show()
   {
   }
}

Create a folder Generates at src/Template and under that folder, create a View file called show.php. Copy the following code in that file.

src/Template/Generates/show.php

<h1>This is CakePHP tutorial and this is an example of Generating URLs<h1>

Execute the above example by visiting the following URL ?

http://localhost/cakephp4/generate

Output

The above URL will produce the following output ?

Generating URL

Redirect Routing

Redirect routing is useful, when we want to inform client applications that, this URL has been moved. The URL can be redirected using the following function ?

static Cake\Routing\Router::redirect($route, $url, $options =[])

There are three arguments to the above function as follows ?

  • A string describing the template of the route.

  • A URL to redirect to.

  • An array matching the named elements in the route to regular expressions which that element should match.

Example

Make Changes in the config/routes.php file as shown below. Here, we have used controllers that were created previously.

config/routes.php

<?php
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
$routes->setRouteClass(DashedRoute::class);
$routes->scope('/', function (RouteBuilder $builder) {
   // Register scoped middleware for in scopes.
   $builder->registerMiddleware('csrf', new CsrfProtectionMiddleware([
      'httpOnly' => true,
   ]));
   $builder->applyMiddleware('csrf');
   $builder->connect('/generate',['controller'=>'Generates','action'=>'show']);
   $builder->redirect('/redirect','https://tutorialspoint.com/');
   $builder->fallbacks();
});

Execute the above example by visiting the following URLs.

URL 1 ? http://localhost/cakephp4/generate

Output for URL 1

Execute URL

URL 2 ? http://localhost/cakephp4/redirect

Output for URL 2

You will be redirected to https://tutorialspoint.com

以上がCakePHP ルーティングの詳細(xì)內(nèi)容です。詳細(xì)については、PHP 中國(guó)語 Web サイトの他の関連記事を參照してください。

このウェブサイトの聲明
この記事の內(nèi)容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰屬します。このサイトは、それに相當(dāng)する法的責(zé)任を負(fù)いません。盜作または侵害の疑いのあるコンテンツを見つけた場(chǎng)合は、admin@php.cn までご連絡(luò)ください。

ホットAIツール

Undress AI Tool

Undress AI Tool

脫衣畫像を無料で

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード寫真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

寫真から衣服を削除するオンライン AI ツール。

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡(jiǎn)単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中國(guó)語版

SublimeText3 中國(guó)語版

中國(guó)語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強(qiáng)力な PHP 統(tǒng)合開発環(huán)境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

PHPで現(xiàn)在のセッションIDを取得する方法は? PHPで現(xiàn)在のセッションIDを取得する方法は? Jul 13, 2025 am 03:02 AM

PHPで現(xiàn)在のセッションIDを取得する方法は、session_id()関數(shù)を使用することですが、session_start()を呼び出して正常に取得する必要があります。 1。Session_start()を呼び出してセッションを開始します。 2。Session_Id()を使用してセッションIDを読み取り、ABC123DEF456GHI789に似た文字列を出力します。 3.返品が空の場(chǎng)合は、Session_start()が欠落しているかどうか、ユーザーが初めてアクセスするか、セッションが破壊されるかを確認(rèn)します。 4.セッションIDは、ロギング、セキュリティ検証、およびクロスレクエスト通信に使用できますが、セキュリティに注意する必要があります。セッションが正しく有効になり、IDが正常に取得できることを確認(rèn)してください。

PHPストリングからサブストリングを取得します PHPストリングからサブストリングを取得します Jul 13, 2025 am 02:59 AM

PHP文字列からサブストリングを抽出するには、Substr()関數(shù)を使用できます。これはSyntax substr(String $ string、int $ start、?int $ length = null)であり、長(zhǎng)さが指定されていない場(chǎng)合は、端まで傍受されます。中國(guó)語などのマルチバイト文字を処理する場(chǎng)合、MB_Substr()関數(shù)を使用して、文字化けコードを避ける必要があります。特定のセパレーターに従って文字列を傍受する必要がある場(chǎng)合は、exploit()を使用するか、strpos()とsubstr()を組み合わせて、ファイル名拡張子またはドメイン名を抽出するなどの実裝できます。

PHPコードの単體テストをどのように実行しますか? PHPコードの単體テストをどのように実行しますか? Jul 13, 2025 am 02:54 AM

unittestinginphpinvolvevidevifignivision like like fike fikionsionsormethodstocatchsearlyandensureliablerefactoring.1)setupphpunitviacomposer、createatestdirectory、and configureautoloadandphpunit.xml.2)

文字列をPHPの配列に分割する方法 文字列をPHPの配列に分割する方法 Jul 13, 2025 am 02:59 AM

PHPでは、最も一般的な方法は、exploit()関數(shù)を使用して文字列を配列に分割することです。この関數(shù)は、指定された區(qū)切り文字を介して文字列を複數(shù)の部分に分割し、配列を返します。構(gòu)文はエクスプロイト(セパレーター、文字列、制限)であり、セパレーターはセパレーターであり、文字列は元の文字列であり、制限はセグメントの最大數(shù)を制御するオプションのパラメーターです。たとえば、$ str = "Apple、Banana、Orange"; $ arr = Explode( "、"、$ str);結(jié)果は["apple"、 "banaです

JavaScriptデータ型:プリミティブ対參照 JavaScriptデータ型:プリミティブ対參照 Jul 13, 2025 am 02:43 AM

JavaScriptデータ型は、プリミティブタイプと參照タイプに分割されます。プリミティブタイプには、文字列、數(shù)字、ブール、ヌル、未定義、シンボルが含まれます。値は不変であり、コピーは値を割り當(dāng)てるときにコピーされるため、互いに影響を與えません。オブジェクト、配列、関數(shù)などの參照タイプはメモリアドレスを保存し、同じオブジェクトを指す変數(shù)は互いに影響します。 TypeofとInstanceOFを使用してタイプを決定できますが、TypeOfNullの歴史的な問題に注意してください。これらの2種類の違いを理解することは、より安定した信頼性の高いコードを書くのに役立ちます。

c c Jul 15, 2025 am 01:30 AM

STD :: Chronoは、現(xiàn)在の時(shí)間の取得、実行時(shí)間の測(cè)定、操作時(shí)點(diǎn)と期間の測(cè)定、分析時(shí)間のフォーマットなど、時(shí)間の処理にCで使用されます。 1。STD:: Chrono :: System_Clock :: now()を使用して、現(xiàn)在の時(shí)間を取得します。 2。STD:: CHRONO :: STEADY_CLOCKを使用して実行時(shí)間を測(cè)定して単調(diào)さを確保し、DurateR_CASTを通じてミリ秒、秒、その他のユニットに変換します。 3。時(shí)點(diǎn)(Time_Point)と期間(期間)は相互運(yùn)用可能ですが、ユニットの互換性と時(shí)計(jì)エポック(エポック)に注意を払う必要があります

PHPの別のページにセッション変數(shù)を渡す方法は? PHPの別のページにセッション変數(shù)を渡す方法は? Jul 13, 2025 am 02:39 AM

PHPでは、セッション変數(shù)を別のページに渡すために、キーはセッションを正しく開始し、同じ$ _Sessionキー名を使用することです。 1.各ページにセッション変數(shù)を使用する前に、session_start()と呼ばれ、スクリプトの前面に配置する必要があります。 2。$ _Session ['username'] = 'Johndoe'などのセッション変數(shù)を設(shè)定します。 3。別のページでsession_start()を呼び出した後、同じキー名を介して変數(shù)にアクセスします。 4.各ページでsession_start()が呼び出されることを確認(rèn)し、事前にコンテンツの出力を避け、サーバーのセッションストレージパスが書き込み可能であることを確認(rèn)してください。 5.SESを使用します

PHPは環(huán)境変數(shù)をどのように処理しますか? PHPは環(huán)境変數(shù)をどのように処理しますか? Jul 14, 2025 am 03:01 AM

toaccessenvironmentvariablesinphp、usegetenv()または$ _envsuperglobal.1.getenv( 'var_name')retievessaspecificvariable.2。$ _ en v ['var_name'] AccessESSESESSVARIABLESIFVARIABLES_ORDERINPHP.INIINCLUDES "E" .SETVARIABLESVIACLIWITHVAR = ValuePhpscript.php、inapach

See all articles