0

I'm getting a MethodNotAllowedHttpExceptions after trying to add a helper library to laravel 4. I've created a class within app/libraries/Time.php, I have added the 'libraries' folder to the composer.json file, under the classmap.

In my global.php file I've added: app_path().'/libraries' to the addDirectories array. Afterwards I did the ./composer.phar dump-autoload.

And under my loginController is where I'm trying to make use of the class. The class in full is below this.

app/libraries/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];
}
}

I'm curious as to how I can keep the httpexception from happening and where I might be going wrong in creating this library.

UPDATE: For people wondering, the loginController is a resource controller and I'm calling the class in store() like so: Time::store($zone); It's this one line that's causing the error.

2 Answers 2

1

I'm not sure if it's causing your problem but set isn't a static method in your class. Try this:

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];
}
3
  • You my friend are the man. That was it. I wasn't sure how to call get without using $this->get. But both those functions not being static is what caused it. This solved it.
    – jamadri
    Sep 24, 2014 at 22:29
  • No worries, glad it helped. You might want to look at putting stuff like this in a 'service provider'. There's a good article here: fideloper.com/create-facade-laravel-4
    – RobbieP
    Sep 24, 2014 at 22:32
  • Thanks. I really appreciate the link. I'll take a look at it and possibly change it around to make it work. Right now though, deadlines :P
    – jamadri
    Sep 25, 2014 at 13:23
0

The error MethodNotAllowedHttpException is related to the HTTP method you are using in your route:

So if you have:

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

Laravel will not accept a POST to that route, you need to create a

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

You might also have an ambiguous route: a route wich was not supposed to be hit, is being hit instead of the other one. This could cause a route collision:

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

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

If you try to hit /posts it will hit your first get and not the resource.

3
  • @Antonio_Carlos_Ribeiro The route is a post. It's a Resource controller and it's being called in the loginController under the store() function. So it should be a post route. Also, the route works without this one line: Time::set($zone);
    – jamadri
    Sep 24, 2014 at 22:04
  • Edited to add another possibility. Sep 24, 2014 at 22:09
  • So I did a ./artisan routes to check the routes under my app. I'm not seeing any collision but just incase, I uploaded my routes to pastebin here: laravel.io/bin/mJQ39. POST portal/store is the one that's causing the issue.
    – jamadri
    Sep 24, 2014 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.