Get the portion of the URL that comes after specific url
use Illuminate\Support\Str;
$url = "http://wz-account-admin-ms/influencer/login";
$dataAfterLastSlash = Str::after($url, 'http://wz-account-admin-ms/');
$desiredSegment = Str::before($dataAfterLastSlash, '/');
echo $desiredSegment;
In blade page
Route is defined in blade page
<div class="row text-center">
<div class="col-lg-12 col-12">
<span><a target="_blank" href="{{ route('publishers_register')}}"><button class="hirbt3"><b><i class="fa fa-registered"></i>
Register</b></button></a></span>
<span><a target="_blank" href="{{ route('publishers_login')}}"><button class="hirbt2"><b><i class="fa fa-sign-in"></i>
Login</b></button></a></span>
</div>
</div>
Route is defined in web.php
when i click route
Route::get('/influencer/login', [App\Http\Controllers\AuthController::class, 'loginForm'])->name('influencer_login');
Route::get('/publishers/login', [App\Http\Controllers\AuthController::class, 'loginForm'])->name('publishers_login');
In controller function is defined
public function loginForm()
{
Log::info('we are in loginForm form');
$currentUrl = request()->url();
log::info($currentUrl);
$dataAfterLast = Str::afterLast($currentUrl, '/');
log::info($dataAfterLast);
$dataAfterLastSlash = Str::after($currentUrl, 'http://wz-account-admin-ms/');
log::info($dataAfterLastSlash);
$desiredSegment = Str::before($dataAfterLastSlash, '/');
log::info($desiredSegment);
if($desiredSegment=="influencer")
{
log::info("ur desired segment");
return view('auth.influencer_login');
}
else
{
return view('auth.publisher_login');
}
}
my log file
[2023-12-06 12:43:29] local.INFO: we are in loginForm form
[2023-12-06 12:43:29] local.INFO: http://wz-account-admin-ms/publishers/login
[2023-12-06 12:43:29] local.INFO: login
[2023-12-06 12:43:29] local.INFO: publishers/login
[2023-12-06 12:43:29] local.INFO: publishers
output
summary
use Illuminate\Support\Str;
$url = "http://wz-account-admin-ms/home";
$dataAfterLastSlash = Str::afterLast($url, '/');
echo $dataAfterLastSlash;
use Illuminate\Support\Str;
$url = "http://wz-account-admin-ms/influencer/login";
$dataAfterLastSlash = Str::after($url, 'http://wz-account-admin-ms/');
$desiredSegment = Str::before($dataAfterLastSlash, '/');
echo $desiredSegment;
Summary
Get the portion of the URL that comes after specific url using Str::after and Str::before
if($desiredSegment=="influencer") then redirect
Top comments (0)