Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save kevin50406418/355e37805139d7b812f59923c41d8a8c to your computer and use it in GitHub Desktop.

Select an option

Save kevin50406418/355e37805139d7b812f59923c41d8a8c to your computer and use it in GitHub Desktop.

Revisions

  1. @phillipsharring phillipsharring revised this gist Oct 6, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.txt
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ an arbitrary URI.

    The your.com/something-else link would go somewhere else and do something different.

    Use it for pretty URLs, shor links, you name it. Use your imagination.
    Use it for pretty URLs, your CMS, short links, you name it. Use your imagination.

    It's not perfect. I haven't figured out if/how route model binding works here. I don't think
    it can, honestly, so your controllers would have to act accordingly. Alternatively, you could
  2. @phillipsharring phillipsharring revised this gist Oct 6, 2015. 1 changed file with 10 additions and 4 deletions.
    14 changes: 10 additions & 4 deletions readme.txt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    Make a migration to add the routes table to your database. Use the Schema::create statement in create_routes_table.php
    Make a migration to add the routes table to your database.
    Use the Schema::create statement in create_routes_table.php

    Values will be like so:

    @@ -10,12 +11,17 @@ Values will be like so:
    | 3 | something-else | ArbitraryController@redirect | "a:1:{s:3:""foo"";s:3:""bar"";}" |
    +----+----------------+------------------------------+-----------------------------------+

    Now, your.com/about will show the about page. Presumably PagesController@show will pull the content from a database using a Page model.
    Now, your.com/about will show the about page. Presumably PagesController@show will pull
    the content from a database using a Page model.

    And your.com/about-us will redirect. PagesController@redirect would simply redirect to an arbitrary URI.
    And your.com/about-us will redirect. PagesController@redirect would simply redirect to
    an arbitrary URI.

    The your.com/something-else link would go somewhere else and do something different.

    Use it for pretty URLs, shor links, you name it. Use your imagination.

    It's not perfect. I haven't figured out if/how route model binding works here. I don't think it can, honestly, so your controllers would have to act accordingly. Alternatively, you could flesh out the map function with some logic to do the bindings there. Finally, I'm not sure if this is a good place to put it. Suggestions/improvements welcome. I'm learning; It's something.
    It's not perfect. I haven't figured out if/how route model binding works here. I don't think
    it can, honestly, so your controllers would have to act accordingly. Alternatively, you could
    flesh out the map function with some logic to do the bindings there. Finally, I'm not sure
    if this is a good place to put it. Suggestions/improvements welcome. I'm learning; It's something.
  3. @phillipsharring phillipsharring revised this gist Oct 6, 2015. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions readme.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    Make a migration to add the routes table to your database. Use the Schema::create statement in create_routes_table.php

    Values will be like so:

    +----+----------------+------------------------------+-----------------------------------+
    | id | uri | action | params |
    +----+----------------+------------------------------+-----------------------------------+
    | 1 | about | PagesController@show | "a:1:{s:5:""pages"";i:1;}" |
    | 2 | about-us | PagesController@redirect | "a:1:{s:2:""to"";s:5:""about"";}" |
    | 3 | something-else | ArbitraryController@redirect | "a:1:{s:3:""foo"";s:3:""bar"";}" |
    +----+----------------+------------------------------+-----------------------------------+

    Now, your.com/about will show the about page. Presumably PagesController@show will pull the content from a database using a Page model.

    And your.com/about-us will redirect. PagesController@redirect would simply redirect to an arbitrary URI.

    The your.com/something-else link would go somewhere else and do something different.

    Use it for pretty URLs, shor links, you name it. Use your imagination.

    It's not perfect. I haven't figured out if/how route model binding works here. I don't think it can, honestly, so your controllers would have to act accordingly. Alternatively, you could flesh out the map function with some logic to do the bindings there. Finally, I'm not sure if this is a good place to put it. Suggestions/improvements welcome. I'm learning; It's something.
  4. @phillipsharring phillipsharring revised this gist Oct 6, 2015. No changes.
  5. @phillipsharring phillipsharring revised this gist Oct 6, 2015. No changes.
  6. @phillipsharring phillipsharring revised this gist Oct 6, 2015. 2 changed files with 14 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion RouteServiceProvider.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    <?php

    // ...

    /**
    * Define the routes for the application.
    *
    @@ -14,7 +18,6 @@ public function map(Router $router)
    $dbRoute = Route::where('uri', '=', $uri)->first();

    if ($dbRoute) {
    /* @var \Illuminate\Routing\Route $route */
    $router->get($uri, function() use ($dbRoute) {
    $segments = explode('@', $dbRoute->action);
    $controller = $segments[0];
    10 changes: 10 additions & 0 deletions create_routes_table.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    <?php

    // ...

    Schema::create('routes', function (Blueprint $table) {
    $table->increments('id');
    $table->string('uri');
    $table->string('action');
    $table->string('params');
    });
  7. @phillipsharring phillipsharring created this gist Oct 6, 2015.
    29 changes: 29 additions & 0 deletions RouteServiceProvider.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    /**
    * Define the routes for the application.
    *
    * @param \Illuminate\Routing\Router $router
    * @return void
    */
    public function map(Router $router)
    {
    $router->group(['namespace' => $this->namespace], function ($router) {
    require app_path('Http/routes.php');

    if (!$this->app->runningInConsole()) {
    $uri = substr($this->app->request->getRequestUri(), 1);
    $dbRoute = Route::where('uri', '=', $uri)->first();

    if ($dbRoute) {
    /* @var \Illuminate\Routing\Route $route */
    $router->get($uri, function() use ($dbRoute) {
    $segments = explode('@', $dbRoute->action);
    $controller = $segments[0];
    $method = $segments[1];
    $obj = $this->app->make($this->namespace . '\\' . $controller);
    $params = (!empty($dbRoute->params)) ? unserialize($dbRoute->params) : [];
    return call_user_func_array([$obj, $method], $params);
    });
    }
    }
    });
    }