Let's say I have the following:
Route::group(array('domain' => array('admin.example.com')), function()
{
...
});
Route::group(array('domain' => array('app.example.com')), function()
{
...
});
Route::group(array('domain' => array('dev.app.example.com')), function()
{
...
});
Is there any way to have multiple domains share a routing group? Something like:
Route::group(array('domain' => array('dev.app.example.com','app.example.com')), function()
{
...
});
Laravel does not seem to support this.
I'm not sure why I didn't think of this sooner, but I guess one solution would be to just declare the routes in a separate function as pass it to both route groups.
Route::group(array('domain' => 'admin.example.com'), function()
{
...
});
$appRoutes = function() {
Route::get('/',function(){
...
});
};
Route::group(array('domain' => 'app.example.com'), $appRoutes);
Route::group(array('domain' => 'dev.app.example.com'), $appRoutes);
I'm not sure if there is any significant performance impact to this solution.
Laravel 5.1
Route::pattern('subdomain', '(dev.app|app)');
Route::group(['domain' => '{subdomain}.example.com'], function () {
...
});
Route::pattern('subdomain', '(dev.app|app)');
Route::pattern('domain', '(example.com|example.dev)');
Route::group(['domain' => '{subdomain}.{domain}'], function () {
...
});
You can pass on the domain name as well:
Route::pattern('domain', '(domain1.develop|domain2.develop|domain.com)');
Route::group(['domain' => '{domain}'], function() {
Route::get('/', function($domain) {
return 'This is the page for ' . $domain . '!';
});
});
Just in case you need to know with which domain name the controller is called.
Tested it with Laravel 5.6.
Interested in this also! I'm trying to register a local development + production subdomain route, for the one controller action.
i.e.
# Local Dev
Route::group(array('domain' => "{subdomain}.app.dev"), function() {
Route::get('/{id}', 'SomeController#getShow');
});
# Production Server
Route::group(array('domain' => "{subdomain}.app.com"), function() {
Route::get('/{id}', 'SomeController#getShow');
});
I tried:
# Failed
Route::group(array('domain' => "{account}.app.{top_level_domain}"), function() {
Route::get('/{id}', 'SomeController#getShow');
});
But it failed.
Not a huge issue, as DesignerGuy mentioned I can just pass in a function to both routes - but it would just be more elegant if they could be grouped :)
check in laravel docs, if you main domain is myapp, in production is myapp.com and in local environment is myapp.dev try using a *
Route::group(array('domain' => '{subdomain}.myapp.*'),
function()
{
...
});
according to laravel document
in laravel 5.4+ you can use this way:
Route::domain('{account}.myapp.com')->group(function () {
Route::get('user/{id}', function ($account, $id) {
//
});
});
A better answer found on Laravel discussion on the same topic is with macros. It works great for me.
https://github.com/laravel/framework/issues/4017#issuecomment-267820459
Route::macro("domain", function(array $domains, \Closure $definition) {
foreach ($domains as $domain) {
Route::group(['domain' => $domain], $definition);
}
});
Route::domain(['foo.bar.dev', 'foo.bar'], function($route) {
// Do stuff
});
Currently you cannot. I had the same 'problem'; my fix is to cycle through your subdomains with a foreach and register the routes.
see this link. http://laravel.com/docs/routing#sub-domain-routing
Route::group(array('domain' => '{subdomain}.example.com'), function()
{
...
});
or Use this package.
https://github.com/jasonlewis/enhanced-router
It help you can set where on group routing like this.
Route::group(array('domain' => '{maindomain}'), function()
{
...
})->where('maindomain', '.+\.example\.com$');
Related
i have some function that check something and give back in some cases "exit();".
i want to use it inside Route::group.
how can i do it right without it impact all the other routes? thanks!
Route::group(['middleware' => ['auth:api']], function() {
myFunction (); //this function can give back: exit();
Route::get('/test', 'Api\Test#test');
});
Turn your function into middleware: https://laravel.com/docs/5.8/middleware
Group the routes that must be affected by your check, and leave out the routes that don't.
Route::group(['middleware' => ['auth:api']], function() {
Route::group(['middleware' => ['MyMiddleware']], function() {
Route::get('/check-me', 'Api\Test#test1');
});
Route::get('/dont-check-me', 'Api\Test#test2');
});
i am new to laravel, i managed to deploy my laravel project on godaddy shared hosting, it works perfectly... but the domain doesnt work...
this is the nature of my routes/web.php
<?php
Route::group(['domain' => 'example.com'], function(){
Route::get('/', function () {
return view('welcome');
});
});
Route::group(['domain' => 'cars.example.com'], function(){
Route::get('/', function () {
return view('cars');
});
});
so, whenever i try accessing cars.example.com, it brings the error "500 internal server error"
guys i need your help on this one, thanks
This works for me but I am not using godaddy.
// All subdomain routes
Route::group(['domain' => 'cars.example.com'], function () {
Route::get('/', function () {
return view('cars');
});
});
// All domain routes
Route::get('/', function () {
return view('welcome');
});
If you want to get fancy you could do this.
// Captures all sub domains
Route::group(['domain' => '{subdomain}.example.{tld}'], function () {
Route::get('/', function () {
return view($subdomain);
});
});
// All domain routes
Route::get('/', function () {
return view('welcome');
});
I just tried Laravel 5 after a time at 4.2..
The docs says it is possible to use 'before' => 'auth' as always, but for me it does not work.
I have no idea whats wrong, I have read the docs, search on internet but seems not to find anything.
My code looks as:
$router->group(['before' => 'auth'], function($router)
{
//
$router->get('admin', function()
{
return View::make('admin.index');
});
//
$router->get('login', function()
{
return View::make('admin.login');
});
});
Anyone can see what I doing wrong here?
In laravel5 filters are removed. Instead you can use middleware classes which are more clean.
In this blog you can read more about the middleware classes and that they're a replacement of filters.
If you want to do it with self written routes you can use this:
Route::group(['middleware' => 'auth'], function()
{
Route::get('admin', function()
{
return View::make('admin.index');
});
Route::->get('login', function()
{
return View::make('admin.login');
});
});
Is it possible to add multiple filters on a group route in Laravel 4?
I have 2 authentification methods for an API centric application.
One with standard authentification (filter "auth" for website), one with token (filter "auth.token" for mobile app).
<?php
Route::group(array('prefix' => 'api/'), function() {
#Custom routes here
});
?>
Ideally I'd like that if one of the two filters pass, group is accessible.
You can:
Route::group(['before' => 'auth|csrf'], function()
{
//
});
However if you want to make it accesible if either of the filters passes, you'd have to write a little bit more (in filters.php):
function csrfFilter()
{
if (Session::token() != Input::get('_token'))
{
throw new Illuminate\Session\TokenMismatchException;
}
}
function authFilter()
{
if (Auth::guest()) return Redirect::guest('login');
}
Route::filter('csrf-or-auth', function ()
{
$value = call_user_func('csrfFilter');
if ($value) return $value;
else return call_user_func('authFilter');
});
In routes.php
Route::group(['before' => 'csrf-or-auth'], function()
{
//
});
Remember you have to return nothing when the filter passes.
I hope this helps you!
You can do that with laravel
Route::group(array('prefix' => 'api/', 'before' => 'filter1|filter2'), function()
{
Route::get('api1', function()
{
// Has Filter1 and filter2
});
Route::get('api2', function()
{
// Has filter1 and filter2
});
});
check the documentation for more details
I have routes like this:
Route::group(array('before' => 'installed'), function() {
Route::group(array('before' => 'auth_admin', 'prefix' => 'admin'), function()
{
Route::group(array('prefix' => 'gag'), function() {
Route::get('/', 'Admin\\GagController#index');
Route::get('delete/{id}','Admin\\GagController#delete');
});
});
});
I need to prevent users from deleting content on my demo application. So I added the following piece of code before my actual routes.
if(App::environment() === 'demo')
{
Route::get('admin/gag/delete/{id}', function() {
die("You can't delete anything on demo application.");
});
}
//Actual routes are at the below.
However, it doesn't work when Route::get('delete/{id}','Admin\\GagController#delete'); is there. Somehow, Laravel ignores my if block and priorities this route. (Although if block is at the top.)
Looks like routes.php parses my if block after routes are parsed.
How can I make it so Laravel prioritizes my demo routes? I just want to restrict access to some features like this.
Ps. I don't want to add all the routes in if blocks. I just want the routes in my if block to be prioritized.
Route filters are better to do this sort of restrictions:
Route::filter('checkDemo', function()
{
if (App::environment() === 'demo')
{
return Redirect::to('home')->withMessage('You can''t delete anything on demo application.');
}
});
And set the filter to your route:
Route::group(array('prefix' => 'gag', 'before' => 'checkDemo'), function()
{
...
});
Or you can filter just that particular route:
Route::get('delete/{id}', array('before' => 'checkDemo', 'uses' => 'Admin\\GagController#delete'));