lararvelについて

このQ&Aのポイント
  • laravelについての情報が古く、うまくいかない場合、web.phpにルーティングの設定を記載してみましょう。
  • laravelのバージョン5.5以上では、web.phpにルーティングの設定を追加することでうまく動作することがあります。試してみましょう。
  • 古い情報に頼らず最新のバージョンのlaravelについて学ぶことが重要です。web.phpに適切なルーティングの設定を行いましょう。
回答を見る
  • ベストアンサー

lararvelについて

http://libro.tuyano.com/index3?id=5486003&page=3 を参考にlararvelをやっているのですが、情報が古いのかうまくいかなかったのでweb.phpに Route::get('/hello', function () { return '<html><body><h1>Hello World</h1><p>this is sample page.</p></body></html>'; }); と記載してやっとうまく表記できました。 所が今度は Route::get('/helo', function () { return view('/helo',['message' => 'Hello!']); }); をweb.phpに記載してもうまくいきません。 5.5ではかわりにここに記載すれば良いのではないでしょうか?

  • PHP
  • 回答数1
  • ありがとう数2

質問者が選んだベストアンサー

  • ベストアンサー
  • smcss
  • ベストアンサー率63% (7/11)
回答No.1

view()で表示する場合、テンプレートを記述します。 return view( 'helo', ['message' => 'Hello!'] );// '/helo' ではなく 'helo' と書くのであれば resources/views/ 内に helo.blade.php を作成 helo.blade.php 以下のようにすると message が Hello に置き換わります。 <html> <body>{{$message}}</body> フレームワークはバージョンで記述が変わったりするのでまずは公式ドキュメントを参照してください。 https://laravel.com/docs/5.5/views https://readouble.com/laravel/5.5/ja/views.html

okweb12345
質問者

お礼

<!doctype html> <html> <head> <title>Sample</title> <style> body { color:gray; } h1 { font-size:18pt; font-weight:bold; } </style> </head> <body> <?php $message = 'dai'; $array = ['dai' => 'debu', 'dai2' => 'debu22']; echo($array['dai2']); ?> <h1>Sample</h1> <p><?php echo $message; ?></p> </body> <?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () { return view('welcome'); }); Route::get('/hello', function () { return '<html><body><h1>Hello World</h1><p>this is sample page.</p></body></html>'; }); Route::get('helo', function () { return view('helo',['message' => 'Hello!']); }); ありがとうございます。上記のようにしたところうまくいったようです。 /を外すと確かにうまくいったのですが、なぜフレームワークは、相対パスではうまくいかないのでしょうか? Route::get('/hello', function () { return '<html><body><h1>Hello World</h1><p>this is sample page.</p></body></html>'; }); には/があるのにうまくいくことも混乱する原因です。 パスが通っているのでファイル名だけで実行できるのでしょうか? どこをカレントディレクトリとして考えているのかも謎ですね。 また、5.5の説明サイトが見つからずに困っていたので、ありがとうございます。 意訳のようなので、若干日本語がおかしいですが、恐らく5.5の情報は、このようなサイトしかないのでしょうね。 http://ysktec.com/shiguregaki/blog/?p=1088 こちらは5.3移行という事で更新も2017年に行っているようなのでディレクトリ構成も5.5対応かと思ったのですが、こちらの情報も古くて使えないのですかね。

関連するQ&A

  • lararvelについて

    lararvelについて http://libro.tuyano.com/index3?id=5486003&page=2 を参考に行っているのですが、 このように Route::get('/hello', function () { return '<html><body><h1>Hello World</h1><p>this is sample page.</p></body></html>'; }); を追加しました。 これは http://localhost:8000/hello にアクセスすると自分がwebapi.phpで指定した記述が表記される ここに記述するのはphpファイルのパスではなく表記するhtmlそのものである所が、フレームワークの特徴という認識で良いでしょうか?

    • 締切済み
    • PHP
  • フレームワークは、相対パスではうまくいかないのでし

    https://okwave.jp/qa/q9374798.html の続き >>> <!doctype html> <html> <head> <title>Sample</title> <style> body { color:gray; } h1 { font-size:18pt; font-weight:bold; } </style> </head> <body> <?php $message = 'dai'; $array = ['dai' => 'debu', 'dai2' => 'debu22']; echo($array['dai2']); ?> <h1>Sample</h1> <p><?php echo $message; ?></p> </body> <?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () { return view('welcome'); }); Route::get('/hello', function () { return '<html><body><h1>Hello World</h1><p>this is sample page.</p></body></html>'; }); Route::get('helo', function () { return view('helo',['message' => 'Hello!']); }); ありがとうございます。上記のようにしたところうまくいったようです。 /を外すと確かにうまくいったのですが、なぜフレームワークは、相対パスではうまくいかないのでしょうか? Route::get('/hello', function () { return '<html><body><h1>Hello World</h1><p>this is sample page.</p></body></html>'; }); には/があるのにうまくいくことも混乱する原因です。 パスが通っているのでファイル名だけで実行できるのでしょうか? どこをカレントディレクトリとして考えているのかも謎ですね。 また、5.5の説明サイトが見つからずに困っていたので、ありがとうございます。 意訳のようなので、若干日本語がおかしいですが、恐らく5.5の情報は、このようなサイトしかないのでしょうね。 http://ysktec.com/shiguregaki/blog/?p=1088 こちらは5.3移行という事で更新も2017年に行っているようなのでディレクトリ構成も5.5対応かと思ったのですが、こちらの情報も古くて使えないのですかね。

    • ベストアンサー
    • PHP
  • ララベルが難しいです。

    ララベルが難しいです。 下記を参考に行っているのですが、 こちらを行っていたのですが、hellowとファイル名を代えて同じようにしたのですが、 http://libro.tuyano.com/index3?id=5486003 http://localhost:8000/helow.phpにアクセスすると Sorry, the page you are looking for could not be found. と出てきてしまいます。 ・hellow.php Route::get('/hellow', function () { return '<html><body><h1>Hello World</h1><p>this is sample page.</p></body></html>'; }); 上記ファイルつくってアクセスするだけでうまくいくのではないのでしょうか?

    • 締切済み
    • PHP
  • $("#exe").click(hello);

    以下のプログラムに$("#exe").click(hello);があります。  その中に#がありますが、シャープを消すと表示タグを押してもリンク先が出てきません! #のシャープって!何のためにあるのですか? <!DOCTYP html> <html> <head> <meta charset="utf-8"/> <title>最初のプログラム</title> <script type="text/javascript"src="/lib/jquery-1.8.1.min.js"></script> <script type="text/javascript"> $(function(){ $("#exe").click(hello); }); function hello(){ $.get("sample1.php",{},function(resp){ $("#show").text(resp); }); ;} </script> </head> <body> <h2>最初のプログラム</h2> <input type="button" id="exe" value=" 表示 "/> <p id="show"></p> </body> </html>  以上宜しくお願いします。

  • wordpressで投稿記事を3パターン表示したい

    投稿記事を3ファイルに分けて表示したいのですが、うまくいかず全てpage.phpとして表示されます。振り分けることができないのですが、アドバイスよろしくお願いします。 前提として page.php page-secound.php page-third.phpの3ファイルがあり固定ページのテンプレートにしている。 <?php /* Template Name: 画像2タイトル1 Template Post Type: page */ ?> <?php /* Template Name: 画像3タイトル1 Template Post Type: page */ ?> 以下のカスタムフィールド名を各記事に割り振っております。 single_rss_feed1 duuble_rss_feed 2 triple_rss_feed 3 ※1つ目の方法 <!--index.phpの文頭に書くコード--!> <?php if(get_post_meta($post->ID,'single_rss_feed1',true) == 'A'): ?> <?php include(('main-first.php') == 'A'); ?> <?php elseif (get_post_meta($post->ID,'duuble_rss_feed2',true) == 'B'): ?> <?php include(('page-secound.php') == 'B'); ?> <?php elseif (get_post_meta($post->ID,'triple_rss_feed3',true) == 'C'): ?> <?php include(('page-third.php') == 'C'); ?> <?php else: ?> <?php endif; ?> _______________________________ ※2つ目の方法 <!--functions.phpに書くコード--!> function custom_template_include($template) { global $post; if (!is_page()) return $template; if (get_post_meta($post->ID, ' duuble_rss_feed 2')) $new_templete = locate_template(array('sub-secound.php')); if (get_post_meta($post->ID, ' triple_rss_feed 3')) $new_template = locate_template( array( 'sub-third.php' )); if (!empty($new_template)) return $new_template; return $new_template; }

    • ベストアンサー
    • PHP
  • PHPが動作しません

    DebianにApache2とPHP5をインストールしました。 初期設定のまま何も設定していない状態です。 w3m http://localhost にてテストページが表示されることは確認できました。 しかし、index.htmlと同じ場所に入れたテストファイル(hello.php)が うまく表示されず、ファイルのダウンロードになってしまいます。 コマンドは w3m http://localhost/hello.php です。 Linuxは初心者で、右も左もわかりません。 (今回も勉強のためです) 設定を確認しようにも、どこをチェックしたらよいのかもわからず、 他のホームページを見てもPHPはデフォルトで動作するように記載があります。 hello.phpの内容です。 <html>  <head>  <title>PHP Test</title>  </head>  <body>  <?php echo '<p>Hello World</p>'; ?>   </body> </html> 少し気になるところは、apach2-mpm-workerというものが、aptitude searchで pになっています。 PHPをインストールする際にiになっていないといけないような記載をみつけたのですが、 PHPと競合してしまい、両方をインストールすることができません。 これが関係しているのでしょうか・・・? ちなみに、それぞれの細かいバージョンは確認の方法がわかりませんので、 「バージョンがわからなけれ答えようがない!」ということであれば、確認方法を お教えください。

    • ベストアンサー
    • PHP
  • laravel でin_array関数使える?

    csvファイルから配列を読み込み view側で条件に当てはまるレコードを 表示させたいと思っています。 下記の通りのcsvファイルとbalde.php Contorllerを用意してcsvの中身を表示させるところまでは 上手くいったのですがphpのin_array関数のようなものを 使って実現出来ないでしょうか? (実現したいこと) ある日付{{\Carbon\Carbon::now()->format("Y/m/d")}}が配列の中に存在すれば表示、存在しなければ 非表示 ↓ソースコード (SampleController.php) <?php namespace App\Http\Controllers; use Illuminate\Http\Request; class SampleController extends Controller { public function csvSample() { $file = new \SplFileObject(storage_path('app/csv_sample_utf8.csv')); $file->setFlags( \SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE ); $lines = []; foreach($file as $line) { $lines[] = $line; } $file = null; $vars = [ 'title' => 'CSV読み込みサンプル', 'lines' => $lines, ]; return view('samples.csv_sample', $vars); } } (csv_sample.blade.php) <h1>{{ $title }}</h1> {{\Carbon\Carbon::now()->format("Y/m/d")}} @foreach($lines as $line) <p>{{ $line[0] }}, {{ $line[1] }}</p> @endforeach (csv_sample_utf8.csv) 2022/1/17,Xデー 2022/5/3,憲法記念日 2022/5/4,みどりの日 2022/5/5,こどもの日 (web.php) <?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\UserController; use App\Http\Controllers\SampleController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () { return view('welcome'); }); Route::get('/user',[UserController::class,'index']); Route::get('/csv_sample', [SampleController::class,'csvSample']);

    • ベストアンサー
    • PHP
  • $queryや$argsの意味

    お世話になります。 wordpressの勉強に色々なサイトを見てサンプルコードを試したりしてますが その中に$queryや$argsといった文字列があったります。 条件にしたがって表示件数を変更のサンプルソースに function set_post_per_page( $query ) { if ( is_admin() || ! $query->is_main_query() ) return; if ( $query->is_home() ) { $query->set( 'posts_per_page', 1 ); return; } if ( $query->is_post_type_archive( 'movie' ) ) { $query->set( 'posts_per_page', 50 ); return; } } add_action( 'pre_get_posts', 'set_post_per_page'); というソースがありました。 これはホームの場合は全件表示し、movieのアーカイブページは50件表示にするソースだと思いますが $queryや$query->の意味が分かりません。 他にも global $post; $args = array( 'posts_per_page' => 5, 'cat' => 1 ); $myposts = get_posts( $args ); foreach( $myposts as $post ) { setup_postdata($post); ?> <h1><?php the_title(); ?></h1> <?php } wp_reset_postdata(); というソースがありましたがこちらにも$args等入っており理解ができません。 これはどういう意味なのでしょうか。 よろしくお願いします。

    • ベストアンサー
    • CSS
  • 同じコードを使っているのにエラーはなぜ?

    ●質問の意味 先日、似たような質問をいたしました。 「未定義の変数」の定義について(PHP) http://okwave.jp/qa/q8060182.html それと同じようなコードを書いていますが、 エラーが出ます。なぜでしょうか? ご存知の方、よろしくお願いします。 ●質問の補足 下記のコードの3行目 $page = isset($_GET['page']) ? intval($_GET['page']) : 1; についてエラーが出ています。 コメントアウトしている //$page =$_REQUEST['page']; は、参考文献の方のコードですがこちらでもエラーが出ます。 ●参考文献 たにぐちまこと「よくわかるPHPの教科書」(P215)の update.phpファイル ●開発環境 windows8 xammp1.8.1 ●コード(update.php) <?php require('dbconnect.php'); //$page =$_REQUEST['page']; $page = isset($_GET['page']) ? intval($_GET['page']) : 1; if ($page == '') { $page = 1; } $page = max($page, 1); //最終ページを取得する $sql = 'SELECT COUNT(*) AS cnt FROM my_items'; $recordSet = mysql_query($sql); $table = mysql_fetch_assoc($recordSet); $maxPage = ceil($table['cnt'] / 5); $page = min($page, $maxPage); $start = ($page - 1) * 5; $recordSet = mysql_query('SELECT m.name, i. * FROM makers m, my_items i WHERE m.id=i.maker_id ORDER BY id DESC LIMIT ' . $start .',5'); ?> <!DOCTYPE html> <html lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link rel="stylesheet" type="text/css" href="style.css" /> <title>Webシステムをつくる</title> </head> <body> <div id="wrap"> <div id="head"> <h1>トップページ</h1> </div> <div id="content"> <p style="margin-top: 20px"> <table width="100%"> <tr> <th scope="col">ID</th> <th scope="col">メーカー</th> <th scope="col">商品名</th> <th scope="col">価格</th> </tr> <?php while ($table = mysql_fetch_assoc($recordSet)) { ?> <tr> <td><?php print(htmlspecialchars($table['id'])); ?></td> <td><?php print(htmlspecialchars($table['name'])); ?></td> <td><?php print(htmlspecialchars($table['item_name'])); ?></td> <td><?php print(htmlspecialchars($table['price'])); ?></td> </tr> <?php } ?> </table> <ul class="paging"> <?php if ($page > 1) { ?> <li><a href="index.php?page=<?php print($page - 1); ?>">前のページへ</a></li> <?php } else { ?> <li>前のページへ</li> <?php } ?> <?php if ($page < $maxPage) { ?> <li><a href="index.php?page=<?php print($page + 1); ?>">次のページへ</a></li> <?php } else { ?> <li>次のページへ</li> <?php } ?> </ul> </p> </div> <div id="foot"> <p><img src="images/txt_copyright.png" width="136" height="15" alt="(C) H2O Space. MYCOM" /></p> </div> </div> </body> </html>

    • ベストアンサー
    • PHP
  • PHP returnについて

    いつもお世話になっております。 PHPのreturn についてお伺いさせてください。 以下のように外部ファンクションを設けておりまして、本体から渡される値を、ファンクション側で計算させて、値を返す方法なのですが、どうもうまくいきません。 どこら辺が間違っているのでしょう? test_1.php ------------------------------------ require_once('test_2.php'); $a=0; $b=5; $c=5; $a = get_data(b,c); test_2.php ------------------------------------ function get_data($data1,$data2){ $data = $data1+$data2; return $data; } ------------------------------------ こんな感じで、本来であればtest_1.phpの$aにtest_2.phpの$dataの結果を渡したいのですが、どうもうまくいかないのです。 申し訳ありませんが、教えて頂けませんでしょうか。 よろしくお願い致します。

    • ベストアンサー
    • PHP