【環境】
Mac Book
ローカルはXAMPPを使用
CakePHP,XAMMP,MySQLすべて最新版
【やりたいこと】
複数あるページを自由に行き来したい。
【ファイル構造(src/Templete)】
Main
→index.ctp
→test1.ctp
→test2.ctp
【src/Controller/MainController.php】
PHP
1<?php 2namespace App\Controller; 3 4use App\Controller\AppController; 5 6class MainController extends AppController 7{ 8 9 //初期化設定 10 public function initialize() 11 { 12 $this->viewBuilder()->autoLayout(true); 13 $this->viewBuilder()->Layout('Main'); 14 } 15 16 public function index() 17 { 18 } 19 20 public function test1() 21 { 22 } 23 24 public function test2() 25 { 26 } 27 28}
【テンプレート内で使用しているヘッダーのコード(src/Layout/main.ctp)】
HTML
1<header> 2 <h1 id="logo"> 3 <a href=""> 4 <?= $this->Html->image('logo.png',array('width'=>'140','height'=>'20','alt'=>''));?> 5 </a> 6 </h1> 7 8 <nav id="menubar"> 9 <ul> 10 <li><a href="Main">TOPページ<span>test1</span></a></li> 11 <li><a href="Main/test1">テストページ1<span>test1</span></a></li> 12 <li><a href="Main/test2">関連サイトリンク<span>test2</span></a></li> 13 </ul> 14 </nav> 15</header>
【問題点】
「localhost/Cakeapp/Main」でトップページを表示する場合は問題ありませんでした。
そして他のページ、例えばテストページ1(aタグ、リンク先「Main/test1」)に飛ぼうとした場合、遷移アドレスが「localhost/Cakeapp/Main/Main/test1」と重複してしまい、うまく遷移ができませんでした。
【試した方法】
Controllerのfunctionに$this->render()をそれぞれ挿入、ただし無限にアクセスしてしまうため却下。
configのroutes.phpに指定アドレスで動かすControllerなどを指定したが動かず却下。
【まとめ】
現状、DBにTOPのアドレス(ローカルならhttp://localhost/)としてテーブル内に保存しており、ページ表示時にControllerからアドレスを呼び出して、遷移先のアドレスをすべてフル表示にするしかないかなと考えていますが、上記のHTMLの様に簡易的に何度おしてもアドレスが重ならない様にする方法があればと試行錯誤しています。
ネット検索でもページ遷移についてはフォームデータの受け渡しばかり出てくるので、参考になりませんでした。
多分Controllerの書き方か、aタグもしくはroutes.phpの書き方次第ではできるのでしょうか?
ご存知の方がいらっしゃいましたらご共有お願いいたします。
長文失礼いたしました、
回答1件
あなたの回答
tips
プレビュー