问题
In laravel the default timezone is set to UTC in app/config/app.php
. On my users settings page users can select their preferred timezone and this is storing the database.
Currently I have this in my routes.php
to change the timezone from the default UTC to the users timezone.
$timezone = (Auth::user()->userDetail->timezone) ? Auth::user()->userDetail->timezone : Config::get('app.timezone');
date_default_timezone_set($timezone);
Is this the best place to put it? I only use routes.php
for my route grouping, get and posts.
回答1:
In the file app/filters.php,
Do this:
App::before(function($request)
{
$timezone = (Auth::user()->userDetail->timezone) ?
Auth::user()->userDetail->timezone :
Config::get('app.timezone');
date_default_timezone_set($timezone);
DB::statement("SET time_zone = '" . $timezone . "'");
});
As a bonus, we set the timezone of the MySQL connection.
回答2:
I'm just learning laravel, but when I've worked with other frameworks I prefer to put things in a service layer. So for example, if you had a route pointing to the page that the user sets the timezone, that would be wired up to a controller. Lets say that controller was called SettingsController and looked something like this (paraphrased):
class SettingsController {
public function saveSettings() {
$settingsService = new SettingsService();
$settingsService->save( $params );
}
}
Then you'd have the settings service in another class:
class SettingsService {
public function save( $params ) {
// Assuming the settings page could save to multiple places (not all in same table)...
$timezone->set( $params['timezone'] ); // set and save timzeone params
.... // save other params
}
}
Otherwise if timezone and whatever is all in the same table, then save it together. If you merely want to display the timezone without saving it, the same concept applies like so:
class SettingsController {
public function setSettings() {
$settingsService = new SettingsService(); // or timeservice or whatever you want to call it
$settingsService->setSettings( $userInfo );
}
}
Then you'd have the settings service in another class:
class SettingsService {
public function setSettings( $userInfo ) {
// Assuming the settings page could save to multiple places (not all in same table)...
$user->setTimezone( $userInfo['timezone'] ); // set and save timzeone params
.... // save other params
}
}
You could go even crazier and have it access an member timezone instead of directly accessing the array, but that's way overkill for this simple project. And yes the new class is a hard coded dependency and could be injected instead. My point is there any many ways to make this way more complicated than it needs to be.
Apologies if this is not clear. Don't have laravel in front of me, but the general idea is to layer it, though this could be too much overhead for what you're doing. I know Zend had the concept of helpers, though I'm not as familiar with laravel to know if that exists (though I would assume it would).
Hope this helps some.
来源:https://stackoverflow.com/questions/25151722/laravel-4-set-timezone-for-user