Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

yassin98010's avatar

MethodNotAllowedHttpException in RouteCollection.php

Hello when i run this url(127.0.0.1:4444/post/liste) i have this error:

MethodNotAllowedHttpException in RouteCollection.php line 207:

    in RouteCollection.php line 207
    at RouteCollection->methodNotAllowed(array('DELETE')) in RouteCollection.php line 194
    at RouteCollection->getRouteForMethods(object(Request), array('DELETE')) in RouteCollection.php line 142
    at RouteCollection->match(object(Request)) in Router.php line 729
    at Router->findRoute(object(Request)) in Router.php line 652
    at Router->dispatchToRoute(object(Request)) in Router.php line 628
    at Router->dispatch(object(Request)) in Kernel.php line 214
    at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 43
    at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
    at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 125
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55
    at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61
    at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
    at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40
    at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
    at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
    at Pipeline->then(object(Closure)) in Kernel.php line 115
    at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
    at Kernel->handle(object(Request)) in index.php line 53


route.php:

`<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('/', 'WelcomeController@index');


/* $router->group(['middleware' =>['auth', 'admin']], function($router)
{ */
$router->get('home', 'HomeController@index');
/* });
 */
 /*Controller of users*/
$router->resource('user', 'UserController');
/*controllers of articles*/
Route::resource('post', 'PostController' , ['except' => ['show', 'edit', 'update']] );
/* Controller of authentification and password*/
Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);

/***************************protected route************************/
get('protected', ['middleware' => ['auth', 'admin'], function() {
    return "this page requires that you be logged in and an Admin";
}]);
/***************************if the user don't have Admin Acces this route is used******************************/
get('sorry',['as' => 'notadmin', function() {
    return " Sorry this page requires that you be logged in and an Admin count";
}]);
/****************************************************************************************************************/

0 likes
16 replies
mstnorris's avatar

You don't have the route /post/liste defined in your routes.php file!

  1. You'd need to add:
Route::get('post/liste', 'PostsController@liste'); add this above the Route::resource('post' ... )
  1. Then you'd need to add a method liste() in your PostsController.
public function liste() {
    // do whatever here!
}

On a side note, you are using three different ways to define routes! Personally stick with one, you'll make less mistakes if you do.

Route::get( ... );
get( ... );
$router->get( ... );
mstnorris's avatar

@yassin98010 it doesn't look like you've read my answer properly. As per your routes file above, you haven't defined the route. Please show your PostsController file too as I don't think you're responding to the route anyway.

yassin98010's avatar

@mstnorris ok this is my postController :

<?php namespace App\Http\Controllers;

use App\Repositories\PostRepository;
use App\Http\Requests\PostRequest;

class PostController extends Controller {

    protected $postRepository;

    protected $nbrPerPage = 4;

    public function __construct(PostRepository $postRepository)
    {
        $this->middleware('auth', ['except' => 'index']);
        $this->middleware('admin', ['only' => 'destroy']);

        $this->postRepository = $postRepository;
    }

    public function index()
    {
        $posts = $this->postRepository->getPaginate($this->nbrPerPage);
        $links = str_replace('/?', '?', $posts->render());

        return view('posts.liste', compact('posts', 'links'));
    }

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

    public function store(PostRequest $request)
    {
        $inputs = array_merge($request->all(), ['user_id' => $request->user()->id]);

        $this->postRepository->store($inputs);

        return redirect(route('post.index'));
    }

    public function destroy($id)
    {
        $this->postRepository->destroy($id);

        return redirect()->back();
    }

}
yassin98010's avatar

@mstnorris it is white page !! the same problem when i put 127.0.0.1:444/post because normaly this link call the index methode so i have the same problem

mstnorris's avatar

Can you show me your routes.php file now. I suspect that you added the route below the Route::resource. You should add it above.

1 like
yassin98010's avatar

@mstnorris

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('/', 'WelcomeController@index');


/* $router->group(['middleware' =>['auth', 'admin']], function($router)
{ */
Route::get('home', 'HomeController@index');
/* });
 */
 /*Controller of users*/
Route::resource('user', 'UserController');
/*controllers of articles*/
Route::get('post/liste', 'PostController@index');
Route::resource('post', 'PostController' , ['except' => ['show', 'edit', 'update']] );

/* Controller of authentification and password*/
Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);

/***************************protected route************************/
Route::get('protected', ['middleware' => ['auth', 'admin'], function() {
    return "this page requires that you be logged in and an Admin";
}]);
/***************************if the user don't have Admin Acces this route is used******************************/
Route::get('sorry',['as' => 'notadmin', function() {
    return " Sorry this page requires that you be logged in and an Admin count";
}]);
/****************************************************************************************************************/

mstnorris's avatar

In your PostController change your index() method to:

public function index()
{
    dd('here'); 
}

when you go to the URL /post/liste it should print "here".

yassin98010's avatar

@mstnorris yes it print here so the problem may be there in liste.blade.php :

@section('contenu')
    @if(isset($info))
        <div class="row alert alert-info">{{ $info }}</div>
    @endif
    {!! $links !!}
    @foreach($posts as $post)
        <article class="row bg-primary">
            <div class="col-md-12">
                <header>
                    <h1>{{ $post->titre }}</h1>
                </header>
                <hr>
                <section>
                    <p>{{ $post->contenu }}</p>
                    @if(Auth::check() and Auth::user()->admin)
                        {!! Form::open(['method' => 'DELETE', 'route' => ['post.destroy', $post->id]]) !!}
                            {!! Form::submit('Supprimer cet article', ['class' => 'btn btn-danger btn-xs ', 'onclick' => 'return confirm(\'Vraiment supprimer cet article ?\')']) !!}
                        {!! Form::close() !!}
                    @endif
                    <em class="pull-right">
                        <span class="glyphicon glyphicon-pencil"></span> {{ $post->user->name }} le {!! $post->created_at->format('d-m-Y') !!}
                    </em>
                </section>
            </div>
        </article>
        <br>
    @endforeach
    {!! $links !!}
@stop
yassin98010's avatar

this is in th template.blade.php

<!DOCTYPE html>
<html lang="fr">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Mon joli site</title>
        {!! HTML::style('https://netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css') !!}
        {!! HTML::style('https://netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css') !!}
        <!--[if lt IE 9]>
            {{ HTML::style('https://oss.maxcdn.com/libs/html5shiv/3.7.2/html5shiv.js') }}
            {{ HTML::style('https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js') }}
        <![endif]-->
        <style> textarea { resize: none; } </style>
    </head>
  <body>
    <header class="jumbotron">
      <div class="container">
        <h1 class="page-header">{!! link_to_route('post.index', 'Mon joli blog') !!}</h1>
        @yield('header')
      </div>
    </header>
    <div class="container">
      @yield('contenu')
    </div>
  </body>
</html>

but template.blade and liste.blade are not in the same folder

mstnorris's avatar
Level 55

@yassin98010

What directory is your template.blade.php in?

Let's say you have

resources
└── views
    ├── errors
    ├── layouts
    │   └── template.blade.php
    ├── posts
    │   └── liste.blade.php
    └── users

Then in your liste.blade.php file you will need to set extend like so:

@extends('layouts.template')

@section('contenu')

...

@endsection
yassin98010's avatar

thank youuuuuuu i didn't make the @extends() ! now it works very well and it works also without the route: Route::get('post/liste', 'PostController@index');

Please or to participate in this conversation.