创建库时出现Laravel MethodNotAllowedHttpException


Laravel MethodNotAllowedHttpException when creating a library

在尝试将助手库添加到laravel 4之后,我得到了一个MethodNotAllowedHttpException。我在app/librarys/Time.php中创建了一个类,我在classmap下的composer.json文件中添加了"libraries"文件夹。

在我的global.php文件中,我添加了:app_path()/libraries"添加到addDirectories数组。之后我做了/composer.phar转储自动加载。

在我的loginController下面是我试图利用这个类的地方。全班同学都在这下面。

app/librarys/Time.php

class Time {
// convert times to user submitted time
public function set($zone)
{
    // Get user timezone from map
    $timezone = $this->get($zone);
    // set default timezone
    date_default_timezone_set($timezone);
}
// maps the timezone the user gives
private function get($time)
{
    $zone = Array(
        'PST'  => 'America/Los_Angeles',
        'MST'  => 'America/Denver',
        'CST'  => 'America/Chicago',
        'EST'  => 'America/New_York',
        'HST'  => 'America/Adak',
        'AKST' => 'America/Anchorage'
    );
    return $zone[$time];
}
}

我很好奇如何防止httpexception的发生,以及在创建这个库时哪里可能出错。

更新:对于想知道的人来说,loginroller是一个资源控制器,我调用store()中的类,如下所示:Time::store($zone);正是这一行导致了错误。

我不确定它是否导致了您的问题,但set不是您类中的静态方法。试试这个:

public static function set($zone)
{
    // Get user timezone from map
    $timezone = self::get($zone);
    // set default timezone
    date_default_timezone_set($timezone);
}

// maps the timezone the user gives
private static function get($time)
{
    $zone = Array(
        'PST'  => 'America/Los_Angeles',
        'MST'  => 'America/Denver',
        'CST'  => 'America/Chicago',
        'EST'  => 'America/New_York',
        'HST'  => 'America/Adak',
        'AKST' => 'America/Anchorage'
    );
    return $zone[$time];
}

错误MethodNotAllowedHttpException与您在路由中使用的HTTP方法有关:

所以,如果你有:

Route::get('posts', ...);

Laravel不会接受该路由的POST,您需要创建一个

Route::post('posts', ...);

你可能还有一条模棱两可的路线:一条本不应该被击中的路线,却被击中了,而不是另一条。这可能会导致路线碰撞:

Route::get('{name}', ...);
Route::resource('posts', ...);

如果你尝试命中/posts,它将命中你的第一个get,而不是资源。