Laravel 8 routes to controllers SEO friendly URL structure

0 votes

i am trying to come up with a specific url structure in my laravel 8  to catch all the route i use catch-all route at the end of the web.php & then to get the single character token and the ID we manipulate the string provided 

is there any other way to do it  

Route::get('{any}', function($any = null){

    // Break up the url into seperate parts.
    $pieces = explode("-", $any);
    $pieces = array_reverse($pieces);
    $piece =  $pieces[0];

    // Remove the .html
    $piece = substr($piece, 0, -5);

    // Get the two parts of the identifier.
    $id = substr($piece, 1);
    $token = substr($piece, 0, 1);

    // Call correct controller based on the token.
    switch ($token) {
        case "d":
            // HERE I WANT TO PASS THE ID ON TO THE SHOW ACTION OF THE DIRECTORY CONTROLLER 
        break;
        case "p":
            // HERE I WANT TO PASS THE ID ON TO THE SHOW ACTION OF THE POSTS CONTROLLER 
        break;
        default:
            return abort(404);
        break;
    }

});
Feb 16 in Others by Kichu
• 17,220 points
96 views

1 answer to this question.

0 votes

use regex to match the slugs 

again use regex to grab the site but before that make sure to pick up your site in the controller method 

public function methodName($site)
{
    preg_match('/.*(-(d(.*?))\.).*/', $site, $parts); //or something similar, $parts[2] will have what you want
}

answered Feb 17 by narikkadan
• 7,860 points

Related Questions In Others

0 votes
1 answer

nginx clean url with seo friendly file names

server {     listen 80;     server_name example.com;     root   /full/server/path/to/your/cms;     index  index.php;     location / ...READ MORE

answered Feb 14 in Others by narikkadan
• 7,860 points
40 views
0 votes
1 answer

Generator URL Friendly SEO PHP with .htaccess

Options +FollowSymLinks -MultiViews RewriteEngine on RewriteBase / # skip all ...READ MORE

answered Feb 14 in Others by narikkadan
• 7,860 points
408 views
0 votes
1 answer

Can an "SEO Friendly" url contain a unique ID?

beware you can get penalised for duplicate ...READ MORE

answered Feb 17 in Others by narikkadan
• 7,860 points
36 views
0 votes
0 answers

SEO friendly url using php

I wanted to use php to make ...READ MORE

Feb 18 in Others by Kichu
• 17,220 points
20 views
0 votes
1 answer

How to exclude specific folders every time I run 'eb deploy' using AWS EB CLI

If there is not .ebignore file it ...READ MORE

answered Oct 24, 2018 in AWS by Priyaj
• 58,060 points
1,296 views
0 votes
0 answers

Self Created Laravel Link Not Working

I installed laravel 6.0 through composer and ...READ MORE

Dec 3, 2019 in Others by anonymous
• 120 points
206 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,680 points
13,318 views
0 votes
1 answer

PHP Convert String to SEO Friendly Url For Bengali Language Type

$string = preg_replace("/[^a-z0-9]/u", "$separator", $string);  change the ...READ MORE

answered Feb 14 in Others by narikkadan
• 7,860 points
40 views
0 votes
1 answer

Writing SEO friendly url gets infinite loop asp.net

you should know that regex avoid cases ...READ MORE

answered Feb 11 in Others by narikkadan
• 7,860 points
95 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP