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', '.*');
Related
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)
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.
In my web.php file, I created two routes :
Route::get('/{name}', 'PublicController#index')->name('welcome');
Route::get('stats', function () { return route('welcome', 'enrique'); });
My controller looks like:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PublicController extends Controller
{
public function index($name)
{
return view('welcome');
}
}
I already set up a virtual host in my local machine which is http://blog.test
When I Call http://blog.test/stats in my browser, it shows me the content of my homepage. But when I reorganize my twho route in web.php file in that way
Route::get('stats', function () { return route('welcome', 'enrique'); });
Route::get('/{name}', 'PublicController#index')->name('welcome');
It works fine.
Can you please explain why it behaves like that? Thanks
What you have is the same route being overwritten. In order to have them both work, you will have to add something before your custom parameter:
/something/{name}
Otherwise stats is assumed to be value for your parameter name
Laravel route goes to first matched routes, so in your case if it sees /stats
Route::get('/{name}', 'PublicController#index')->name('welcome');
It becomes variable $name for PublicController#index
Check more article regarding this
It is happening because , when you add your {parameter} as after / , all routes defined after that, are considered as of that type
Route::get('/{name}', 'PublicController#index')->name('welcome');
// below routes not work
Route::get('stats', function () {});
Route::get('test', function () { });
Route::get('hello', function () {});
same thing happen if you create new route like below :
Route::get('post/{slug}', function () {});
// this get routes are also not work
Route::get('post/show', function () {});
Route::get('post/preview', function () {});
so it is good practice that always define parameterised route at the
last
Route::get('post/show', function () {});
Route::get('post/preview', function () {});
Route::get('post/{slug}', function () {});
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');
});
});
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