Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

Extracting a segment from a URL in Larvel

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;
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Image description

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');
Enter fullscreen mode Exit fullscreen mode

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');
             }
       }
Enter fullscreen mode Exit fullscreen mode

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  
Enter fullscreen mode Exit fullscreen mode

output

Image description

summary

use Illuminate\Support\Str;

$url = "http://wz-account-admin-ms/home";
$dataAfterLastSlash = Str::afterLast($url, '/');

echo $dataAfterLastSlash;
Enter fullscreen mode Exit fullscreen mode
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;
Enter fullscreen mode Exit fullscreen mode

Image description

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)