Sunday, March 12, 2017

How to create a Route in Laravel to this scenario?

The basic idea is to work like a sub-domain architecture, but the user account will come after website's name.

I have a web service that is called myService.com. I will have users like Paul, Anne, Jhon. I want Laravel to be able to create home pages for Paul, Anne and Jhon like this:

  1. myService.com/Paul = > Should redirects to Paul home page
  2. myService.com/Anne = > Should redirects to Anne home page
  3. myService.com/Jhon = > Should redirects to Jhon home page

but myService.com has its own home page and others pages too. I do like this:

Route::get('/', function () {
    return view('home');//myService.com HOME page
});

Route::group(['prefix'=>'videos'], function (){

    Route::get('/', function () {
        return "all videos";
    });
    Route::get('/{id}', function ($id) {
        return "Video => $id";
    });

});

Route::group(['prefix'=>'articles'], function (){

        Route::get('/', function () {
            return "all articles";
        });
        Route::get('/{id}', function ($id) {
            return "Article => $id";
        });
 });

For the users I will have almost the same URL structure:

myService.com/Paul
myService.com/Paul/videos
myService.com/Paul/videos/id
myService.com/Paul/articles
myService.com/Paul/articles/id

myService.com/Paul2
myService.com/Paul2/videos
myService.com/Paul2/videos/id
myService.com/Paul2/articles
myService.com/Paul2/articles/id

Since I will have unlimited users account I dont know how to design the Route structure for this scenario.

Is It should be like this?

Route::group(['prefix'=>'account'], function (){

    Route::get('/', function ($account) {
        return "user $account home page";//Ex myService.com/Paul
    });

    Route::group(['prefix'=>'videos'], function (){
        Route::get('/', function () {
           return "user account - all videos";//Ex myService.com/Paul/videos
        });

        Route::get('/{id}', function ($id) {
           return "user account video => $id";//Ex myService.com/Paul/videos/id
        });
    });

    Route::group(['prefix'=>'articles'], function (){
        Route::get('/', function () {
           return "user account - all articles";//Ex myService.com/Paul/articles
        });

        Route::get('/{id}', function ($id) {
           return "user account articles => $id";//Ex myService.com/Paul/articles/id
        });
    });

});

This is what I did so far, but I dont know if it is a good approach. It is correct?



via zwitterion

Advertisement