LoginSignup

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

laravel ルートを追加したがリンク先に飛ばない

laravelのリンク生成

例)
laravelで簡単な掲示板を作成するために記事詳細ページ、投稿ページなどの動作確認をしています。
リンク生成の実装中にエラーが発生しました。
解決方法を教えて下さい。

ブラウザでリンクをクリックしたがリンク先に飛ばない状況です。

<web.phpソースコード>

<?php

Route::get('/', function () {
    return redirect('/articles');
});

Route::get('/articles', 'ArticleController@index')->name('article.list');
Route::get('/article/new', 'ArticleController@create')->name('article.new');
Route::get('/article/{id}', 'ArticleController@show')->name('article.show');


<ArticleController.phpソースコード>

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $message = 'Hello world';
        $articles = Article::all();
        return view('index', ['message' => $message, 'articles' => $articles]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $article = new Article();

        $article->content = 'Hello tinker';
        $article->user_name = 'kodmaa';
        $article->save();
        return redirect('/articles');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request, $id, Article $article)
    {
        $message = 'This is your article ' . $id;
        $article = Article::find($id);
        return view('show', ['message' => $message, 'article' => $article]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function edit(Article $article)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Article $article)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function destroy(Article $article)
    {
        //
    }
}

<index.blade.phpソースコード>

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset='utf-8'>
    <title>Web application</title>
    <style>body {padding: 10px;}</style>
</head>
<body>
<h1>Web application</h1>
<p>{{ $message }}</p>
@foreach ($articles as $article)
    <p>
        <a href='{{ route("article.show", ["id" =>  $article->id]) }}'>
            {{ $article->content }},
            {{ $article->user_name }}
        </a>
    </p>
@endforeach
<div>
    <a href={{ route('article.new') }}>新規投稿</a>
</div>
</body>
</html>

<show.blade.php>

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset='utf-8'>
    <title>Web application</title>
    <style>body {padding: 10px;}</style>
</head>
<body>
<h1>Web application</h1>
<p>{{ $message }}</p>
<p>{{ $article->content }}</p>
<p>{{ $article->user_name }}</p>

<p>
    <a href={{ route('article.list') }}>一覧に戻る</a>
</p>
</body>
</html>

例)

web.phpにルートの追加


Route::get('/articles', 'ArticleController@index')->name('article.list');
Route::get('/article/new', 'ArticleController@create')->name('article.new');
Route::get('/article/{id}', 'ArticleController@show')->name('article.show');
0

2Answer

リンクをクリックしてリンク先に飛ばないというエラーというか不具合?でしょうか?
エラー画面は表示されてないんですよね?
ブラウザの検証を使って指定したリンクがどのように表示されているか確認してみましょう。
多分routeの設定は問題ないような気がするので

<a href="{{ route('article.list') }}">

""で囲えばうまく行く気がします

0

ブラウザでURLを直接記述するとリンク先のページは表示されているところに注目しました。
1、index.blade.phpページ、show.blade.phpページ、のURLを確認
2、http://localhost:80/articles=index.blade.phpを表示
     show.blade.phpへ遷移した時のURLは『https://localhost:80/articles/1』
3、リンククリックでページ遷移した時にhttpがhttpsになっている!!
4、心当たりがある箇所ははApp Service Provider.phpに『\URL::forceScheme('https');』の記述
5、httpsをhttpにしたら正常に動作した!!

<詳細>
App Service Provider.phpの中の記述に問題がありました。
下記のような記述をしていたのですが
public function boot()
{
\URL::forceScheme('https');
}
上記の記述のhttpsをhttpに変更したら解決しました。

・試したこと
エラー画面の表示がされていていません。
なので、ブラウザの検証を使って指定したリンクがどのように表示されているかを確認しましたが原因となる箇所が見当たらなかったのでブラウザでURL


class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    \URL::forceScheme('https');
}

}

0

Your answer might help someone💌