检测子域并在laravel中设置共享(全局)变量

时间:2014-09-12 07:12:39

标签: php laravel laravel-routing

我有以下代码段成功路由子域请求。我正在寻找一种方法,我可以检测子域并做一些快速逻辑,根据子域为全局变量赋值。我在哪里可以做到这一点?

Route::group(array('domain' => '{username}.murch.co'), function() {
    Route::controller('checkout', 'CheckoutController');
    Route::get('/', 'UserController@getProfile');
});

我想做点什么

Route::group(array('domain' => '{username}.murch.co'), function() {
    $var = "dog".$username;
    View::share('var', $var);
    Route::controller('checkout', 'CheckoutController');
    Route::get('/', 'UserController@getProfile');
});

2 个答案:

答案 0 :(得分:0)

我仍然不知道如何实现这个是routes.php,但我最终找到了使用过滤器的解决方案

 App::before(function($request)
 {
    $urlParts = explode('.', $_SERVER['HTTP_HOST']);
    $subdomain = $urlParts[0];
    $userModel = new User;
    $user = $userModel->getUserByUsername($subdomain);
    View::share('user', $user);
 });

答案 1 :(得分:0)

对于Laravel 5.x,您可以从任何地方调用它 - 控制器,视图等。

c:\Work\Dev\StackOverflow\q46833364>set PATH=%PATH%;.\external\Win32-Release

c:\Work\Dev\StackOverflow\q46833364>set PYTHONPATH=%PYTHONPATH%;.\ckb\Win32-Release

c:\Work\Dev\StackOverflow\q46833364\>"c:\Install\x86\HPE\OPSWpython\2.7.10__00\python.exe" ckb_test.py

Module: <module 'ckb' from 'c:\Work\Dev\StackOverflow\q46833364\ckb\Win32-Release\ckb.pyd'>
Dir: ['Key', '__doc__', '__file__', '__name__', '__package__']

Class: <type 'ckb.Key'>
Dir: ['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'x']

Instance: <ckb.Key object at 0x027A7050>
Dir: ['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'x']

Key.x (initial): 1618
Key.x (modified): 123
Traceback (most recent call last):
  File "..\ckb_test.py", line 20, in <module>
    key.x = 1.0
TypeError: 'x' value must be an int

End