How to redirect to specific URL? - php

I have a route for tenants name like that:
example.test/TenantNameHere
And I want to make the default of my domain start with the word "site":
example.test/site
My code:
Route::prefix('{tenantName}')->group(function () {
Route::get('/', function ($tenantName) {
return "{$tenantName}";
});
});
Route::prefix('site')->group(function () {
Route::get('price', function () {
return view('welcome');
});
});
Route::redirect('/', 'site', 301);
The problem that I'm facing with this code now is when I open the domain it redirects me to tenantName route, not to the home page that I made!
How can spreate the route of site from TenantName ?

you just have to register your absolute path ('site') first, and "wildcards" routes after, because everything you put in url line now hit the first tenantName route
try this (reverse the order):
Route::redirect('/', '/site', 301);
Route::prefix('site')->group(function () {
Route::get('/', function () {
return view('welcome');
});
});
Route::prefix('{tenantName}')->group(function () {
Route::get('/', function ($tenantName) {
return "{$tenantName}";
});
});
also change "/site/price" path to "/site", so redirect will find correct route

Related

Laravel route parameter except

Trying to make dynamic URL, but I want it to accept the "admin" parameter.
Route::namespace('Web')->group(function () {
Route::namespace('Home')->group(function () {
Route::get('/{sef?}/{sef2?}', 'HomepageController#index')->name('homepage');
});
});
I don't want the code above affects the following code:
Route::group(['middleware' => ['auth']], function () {
Route::prefix('admin')->namespace('CMS')->group(function () {
Route::prefix('user')->namespace('Management')->group(function () {
Route::get('/', 'UserController#profile')->name('user-dashboard');
.......
But when I go to page like; /admin/users or /admin only the route /{sef}/{sef2} works. It doesn't see the /admin routing.

Function inside route group [laravel-passport]

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');
});

Error in View Route Laravel with Virtual Host XAMPP

I'm was change my XAMPP port to 8000 and I open my virtual host with http://laravel.test:8000/
I try to add this to my routes.php :
Route::get('/', function () {
return view('homepage');
});
Route::get('/', function () {
return view('about');
});
but when I'm try to open : http://laravel.test/about:8000/ or http://laravel.test/homepage:8000/ it's error.
How can I open that page with the xampp port changed?
Define your routes properly
Route::get('/', function () {
return view('homepage');
});
http://laravel.test:8000/
Route::get('about', function () {
return view('about');
});
http://laravel.test:8000/about

SubDomain getting Internal server error in laravel 5.3

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');
});

Laravel grouping routes with one name

i have this below route and that can work correctly
Route::get('admin/login', array('as'=>'login', function()
{
return View::make('back_end.login');
}));
app
views
back_end
layouts
index.blade.php
main.blade.php
profile.blade.php
login.blade.php
for admin i have any view for show and i want to grouping that with admin perfix. after this action and use
http://localhost/laravel/public/admin/login
http://localhost/laravel/public/admin/profile
URL i get this error:
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
this is my routes:
Route::group(array('prefix' => 'admin'), function()
{
Route::get('login', function()
{
return View::make('back_end.login');
});
Route::get('index', array('as'=>'dashboard'), function()
{
return View::make('back_end.layouts.index');
});
Route::get('profile', function()
{
return View::make('back_end.layouts.profile');
});
});
how to fix this routes. please help me
I had the same issue recently. Here is a slimmed down version of the routing that I used, including a catch all. I was routing to controllers, however you can replace that syntax with a function, the rout will be handled the same.
Route::group(array('prefix' => 'admin'), function(){
Route::get('/','AdminController#index');
Route::resource('users', 'UserController');
Route::get('settings','AdminController#settings');
/* Catch all route */
Route::any('{all}', function($uri){
return Redirect::to('admin')
->with('flash_error', "The administration page 'admin/$uri' could not be found.");
})->where('all', '.*');
});
As always, make sure to run composer dump-autoload after updating the routes. This worked successfully for me. You will only need the '/' on the relative 'base' route.
Make changes (add a preceding slash / to each routes inside the admin group) as given below:
Route::group(array('prefix' => 'admin'), function()
{
Route::get('/login', function()
{
return View::make('back_end.login');
});
Route::get('/index', array('as'=>'dashboard'), function()
{
return View::make('back_end.layouts.index');
});
Route::get('/profile', function()
{
return View::make('back_end.layouts.profile');
});
});
It should be /login instead of login and same for each one.

Categories