Subdomain Base Without URI , Unable to catch base url - laravel

Route::domain('{account}.myapp.com')->group(function () {
Route::get('/', function ($account) {
dd("Cant hit this area");
});
Route::get('/test', function ($account) {
dd("No problem reaching this area");
});
});
When I visit my subdomain e.g. test.myapp.com it won't hit that DD but if say I do a /test and visit it, it would work.
I tried doing Route::get(' ' too , leaving it empty but it just wouldn't catch the base subdomain.
My Apache Conf : ServerAlias *.example.com
Ref : https://laravel.com/docs/5.8/routing#route-group-sub-domain-routing
Update
I also tried this, couldn't get it to execute
Route::group(['domain' => '{account}.example.co'], function () {
Route::get('/', function ($account) {
dd("HIT");
});
});

Found the solution.
Basically, I had this above the code I posted
Route::get('/', ['uses' => 'MainController#home']);
It was overwriting my current route. I shifted my code above this code so it doesn't get overwritten by it.

Related

Laravel pass subdomain as a value to another route

now I have this code
Route::group(['domain' => '{subdomain}.'.env('DOMAIN')], function()
{
Route::any('/', function($subdomain)
{
Route::get('{/subdomain}/{identifier}/{reCreate?}','AController#index');
});
});
I want to pass {subdomain} to Route::get('{/subdomain}/{identifier}/{reCreate?}','AController#index'); for when call subdomain.localhost/identifier I call AController#index I should pass the value of subdomain to AController#index
I think by using Route::input('subdomain'); you can access the subdomain parameter.
Inside AController:
public function index() {
dd(Route::input('subdomain'));
}
Please try this.
Route::domain('{account}.myapp.com')->group(function () {
Route::get('user/{id}', function ($account, $id) {
//
});
});
I figured out the answer thanks guys for your help ,
the answer is just call the route inside route::group
Route::group(['domain' => '{subdomain}.'.env('DOMAIN')], function()
{
Route::any('/{identifier}/{reCreate?}','AController#index');
});
and inside Controller function index I can access the value as normal function($subdomain)

Redirect entire route group to another url?

Given a route group like Route::group(['prefix' => 'foo',]) how would I redirect all routes under this group to domain.com?
I've tried:
Route::group(['prefix' => 'foo'], function() {
Route::get('/', function () {
return Redirect::to('http://domain.com');
});
});
But that would only redirect myoldsite.com/foo to domain.com, anything such as myoldsite.com/foo/deeper/path would just be a 404. Is there a way to catch all sub routes and redirect them?
You can add a catch-all parameter.
Route::group(['prefix' => 'foo'], function() {
Route::get('/{atsl}', function ($atsl) {
return Redirect::to('http://domain.com');
});
});

get current path outside of Route

In this URL: http://siteName/html/test
I want to catch test, of course inside the route.
This works great:
Route::get('test', function()
{
return Route::getCurrentRoute()->getPath();
});
But I want to access the path outside of any route and in my route.php file like this:
// routes.php
return Route::getCurrentRoute()->getPath();
Route::get('test', function()
{
...
});
In case you try to catch all routes. Add this as your last route.
Route::any('{all}', function($uri)
{
return Route::getCurrentRoute()->getPath();
})->where('all', '.*');

Cannot return properly View with prefix and parameter

I have an route that looks as following:
Route::group(['prefix' => 'show' ], function() {
Route::get('{id}', function($id)
{
return View::make('show')->with('id' , $id);
});
});
When I enter URL etc test.com/show/343242 it returns what it should. But what I want it to redirect to that site. When I use Redirect::to('show/34324'); it simply just give me an blank screen. When I try to use return Redirect::route('show/34324'); it just give me that the route show/34324 don't exist. I have no idea how to proceed, have search around for 1 hour until now.
Hopefully someone can bring some light into this, thanks.
You should return your Redirect
return Redirect::to('show/34324');
To Redirect from a named route
Route::get('{id}', array('as'=>'route-name',function($id)
{
return View::make('show')->with('id' , $id);
}));
// ...
return Redirect::route('route-name',array(34324));

how to use the laravel subdomain routing function

I am using the following code its from the laravel 4 site
Route::group(array('domain' => '{account}.myapp.com'), function() {
Route::get('user/{id}', function($account, $id) {
// ...
return Redirect::to('https://www.myapp.com'.'/'.$account);
});
});
the idea is to redirect subdomain.myapp.com to myapp.com/user/subdomain
.what I have is not working any suggestions?sorry I just started with laravel about a month now.
Remove user/{id} and replace it with / and then use the following url https://accountname.myapp.com and it will redirect to https://www.myapp.com/accountname
Route::group(array('domain' => '{account}.myapp.com'), function() {
Route::get('/', function($account, $id) {
// ...
return Redirect::to('https://www.myapp.com'.'/'.$account);
});
});
Edit the answer to the correct answer
Route::group(array('domain' => '{account}.myapp.com'), function() {
Route::get('/', function($account) {
// ...
return Redirect::to('https://www.myapp.com/'.$account);
});
});
as Marc vd M answer but remove $id from get's closure function.
why use 'https://www.myapp.com'.'/'.$account and not 'https://www.myapp.com/'.$account

Resources