Vấn đề về Route trong Laravel

Hiện tại mình đang tạo một Form để add new blog.

Từ trang localhost:8888/blog/public/post dẫn đến Form bằng đường dẫn localhost:8888/blog/public/post/create . Nhưng khi mình gặp lỗi sau: Sorry, the page you are looking for could not be found.

Code ở file routes/web.php :Route::get('/post', 'PostController@index'); Route::get('/post/{id}', 'PostController@show'); Route::get('/post/create', 'PostController@create');

Ở file PostController :

class PostController extends Controller
{
    public function index() {
        $posts = Post::all() ;
    	return view('post.index', compact('posts')) ;
    }

    public function show(Post $id) {
    	return view('post.show',compact('id')) ;
   }

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

Ở file index:

@extends ('post.layouts.master')


@section ('content')
	<div class="container">
	  <div class="jumbotron">
	  	<h1 style="text-align: center;">Welcome To My Blog</h1>
	  </div>
	</div>
    
    @if(count($posts) > 0)
    	<ul class="list-group">
    		@foreach ($posts as $post)
    			<li class="list-group-item"><a href="post/{{$post->id}}">{{$post->title}}</a></li>
    		@endforeach
		</ul> 
    @endif
    <h2 style="text-align: center;"><a href="post/create">Add new post</a></h2>
@endsection

Cây thư mục view:

Nếu mình sửa như thế này sẽ truy cập được Form:

File index:

<h2 style="text-align: center;"><a href="/create">Add new post</a></h2>

File route:

Route::get('/create', 'PostController@create'); 

Kết Quả:

Code đầu tiên mình làm theo tutorial ở Laracast nhưng chẳng hiểu sao lại bị báo lỗi, có ai giải thích giúp mình vì sao không?

Mình cám ơn trước

Bạn chỉ bắt method get
Nên thẻ li đó là post nó ko nhận

hình như bạn nhầm, ở thẻ a file index mình chuyển sang trang create để hiện form chứ không phải từ form chuyển sang trang khác.

Thử

<h2 style="text-align: center;"><a href="/post/create">Add new post</a></h2

xem sao

không được bạn, nếu sửa vậy thì thanh URL sẽ chuyển từ :
http://localhost:8888/blog/public/post

sang http://localhost:8888/post/create

Vậy lúc lỗi thì đường link là localhost:8888/blog/public/post/create hay localhost:8888/post/create

lỗi thì đường link là : localhost:8888/blog/public/post/create

vậy chạy hẳn đường link localhost:8888/blog/public/post/create vẫn lỗi à b?

1 Like

đúng rồi bạn, nó báo là Sorry, the page you are looking for could not be found.
nhưng khi rút ngắn lại như trên topic nói, vẫn giống đường link, nhưng method create lại được gọi :confused:

Theo suy đoán của mình thì 99% là do Route Model Binding - Implicit Binding nhé.

Route::get('/post/{id}', 'PostController@show');
public function show(Post $id) {
    return view('post.show',compact('id')) ;
}

=> Do bạn type-hint Post $id trong controller nên Laravel sẽ tự động inject Model Post vào method show, có nghĩa là nó sẽ gọi hàm này: $id = Post::find($id), nếu không tìm thấy trả về response HTTP 404 (Ném ra ModelNotFoundException => rồi biến thành NotFoundHttpException => 404)

Khi bạn vào url post/create => Laravel sẽ gọi Post::findl('create'); trước (do nó match cái route khai báo đầu trước) và tất nhiên kết quả là 404!!!

Solution: đổi thứ tự 2 cái route hoặc tắt Implicit Binding hoặc đặt url khác, vd create-post hoặc dùng Resource Controller

Bonus: recommend bạn dùng helper action() hoặc route() để generate url, sau này có thay đổi url thì chỉ cần sửa trong route

3 Likes

Bạn @it-4-life nói quá chuẩn rồi đó =)). Mình cũng nghĩ là vậy, do cấu trúc url của bạn. Và nếu như bạn chỉ làm CRUD thì nên dùng resource controller để làm, nó có đủ hết chỉ với 1 dòng route thôi.
https://tienleit.com/hoc-laravel-framework/laravel-tutorials/resource-controller-trong-laravel-crud-la-chuyen-nho/

1 Like

Ôi được rồi, cám ơn bạn rất nhiều.

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?