Barna Bruder
Barna Bruder

Reputation: 5

Laravel 5 misroute?

I'll copy the relevant code and then i'll explain the problem.

routes.php

get('/movies/create', 'MovieController@create');

Moviecontroller.php

public function create()
{
    return view('movies.create');
}

master.blade.php

<html>
<head>
    <title>Laravel</title>
</head>
<body>
@yield('content')
</body>
</html>

edit.blade.php

@extends('master')

@section('content')
    <h1>Edit {{ $movie->name }}</h1>

    {!! Form::model($movie, ['url' => '/movies/' . $movie->id, 'method' => 'PATCH']) !!}
        {!! Form::text('name') !!}
        {!! Form::text('director') !!}
        {!! Form::textarea('description') !!}
        {!! Form::text('rating') !!}
        {!! Form::submit('Update movie') !!}
    {!! Form::close() !!}
@stop

show.blade.php

@extends('master')

@section('content')
    <h1>{{ $movie->name }}</h1>
    <h4>Director: {{ $movie->director }}</h4>
    <h4>Rating: {{ $movie->rating }}</h4>
    <p>Description: {{ $movie->description }}</p>
    {!! link_to('/movies/' . $movie->id . '/edit', 'Edit') !!}
@stop

so i have this code, but when i go to /movies/create in the browser, it's trying to open show.blade.html, which, of course, will throw an exception ($movie does not exist). Why does that happen?

Upvotes: 0

Views: 58

Answers (2)

Cavid
Cavid

Reputation: 109

best way:

php artisan make:controller MovieController

and define route

Route::resource('movies', 'MovieController');


//available routes
    Verb            Path    
    GET             /movies
    GET             /movies/create   
    POST            /movies
    GET             /movies/{id}          
    GET             /movies/{id}/edit   
    PUT/PATCH       /movies/{id}    
    DELETE          /movies/{id}    

Upvotes: 0

Joel Hinz
Joel Hinz

Reputation: 25384

You probably have a conflicting route above the one you showed, something like this:

get('/movies/{movie}', 'MovieController@show');
get('/movies/create', 'MovieController@create');

So when you go to yoursite.com/movies/create in your browser, the first route is triggered, and the controller opens show.blade.php - but there is no movie for it to show yet.

If you move them the other way around, the create method will work as intended, and you'll still be able to show existing movies:

get('/movies/create', 'MovieController@create');
get('/movies/{movie}', 'MovieController@show');

Upvotes: 1

Related Questions