欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Laravel Sub-Domain Routing with Resource Controllers

I have set a wildcard subdomain and the subdomain name dynamically represent an account. I want to use this account name to check if the user can see a particular resource. I also want to use this account name in several controllers to print custom data about the account.

Route::group(array('domain' => '{account}.mywebsite.com'), function()
{
    Route::group(array('before' => 'check_account'), function() // I want to use $account in check_account filter
    {
        Route::resource('users', 'UserController');
        Route::resource('docs', 'DocController'); // I want to use $account in this controller
    });
});

How can I use the $account variable in a filter and in a method of a resource controller ?

shareimprove this question
 

1 Answer 正确答案

You can access $account in check_account filter like

Route::filter('check_account', function($route){
    $account = $route->getParameter('account');
});

In your DocController you may access the account as

public function index($account)
{
    // $account is available here
}
shareimprove this answer
 

来自  https://stackoverflow.com/questions/20378192/laravel-sub-domain-routing-with-resource-controllers

Here is my route:
Route::group(array('domain'=>'{subdomain}.example.com'), function()  
{
    Route::group(array('before'=>array('authenticate')), function()
    {
        Route::resource('post', 'PostController');

        Route::resource('comments', 'CommentsController');

        Route::resource('forum', 'ForumController');

        Route::resource('users', 'UsersController');

    });
});

It seems that by default ALL my resource controllers are now taking the subdomain as first parameter and I can't find a way to change it.

If possible, I'd like to ignore the subdomain entirely.

Example use cases:

admin.example.com/forum/1?domain=foo

foo.example.com/forum/1

bar.example.com/forum/2

This site hosts 2 forums, but there is only 1 admin managing all of them. For admin to access the forum foo, admin must also supply domain=foo to look at it.
 

Can you post the rest of your routes file? I dont see any other resource controllers other than PostController?– Laurence May 20 '14 at 3:20
   
@TheShiftExchange I've added all of them in – Jonathan Chow May 20 '14 at 3:23
   
Route::group(array('domain'=>'admin.example.com') you can use the rule for admin separately before oute::group(array('domain'=>'{subdomain}.example.com') rule. – Amit Garg May 20 '14 at 3:54
   
@AmitGarg yes, but it seems quite ugly if I have to copy all those resource controllers again for each of those route domain. – Jonathan Chow May 20 '14 at 4:03





 

1 Answer  正确答案

 
 

If you dont want to route based on the subdomain, then you should only filter the subdomain for access.

Route::filter('subdomain', function($route, $request) 
{
    $host = $request->getHost();
    $parts = explode('.', $host);
    $subdomain = $parts[0];

    if ($subdomain == 'something')
    {
         // Allow or deny
    }
});


Route::group(array('before'=>array('subdomain|authenticate')), function()
    {
        Route::resource('post', 'PostController');

        Route::resource('comments', 'CommentsController');

        Route::resource('forum', 'ForumController');

        Route::resource('users', 'UsersController');

    });
shareimprove this answer
 
   
I might not be clear enough. I need to use subdomains for all routes because both normal users and admins will be accessing the same routes. But the subdomain parameter is not important to the functions at all, they just trigger different authentication. – Jonathan Chow May 20 '14 at 3:33
   
Yeah - sorry your not being clear enough to me - I dont understand. Can you please provide some further examples in your question to expand more. – Laurence May 20 '14 at 3:36
   
oh wait - i get it now - give me 2 mins - i'll change my answer – Laurence May 20 '14 at 3:43
   
I've added example use cases in the original question. Hope it's clear enough. – Jonathan Chow May 20 '14 at 3:44
   
Try that - see if it helps. – Laurence May 20 '14 at 3:48

来自  https://stackoverflow.com/questions/23750301/laravel-sub-domain-routing-and-resource-controllers

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

普通分类: