Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Laravel Creating Dynamic Routes to controllers from Mysql database

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 5.83k
    Comment on it

    I want to create dynamic routes to controller from mysql database.We have the available following table for this.

        id   name      route
        --------------------
        0   About       about
        1   Contact     contact
        2   Blog        blog
    

    Now we have to create dynamic routes in routes.php.

    This table is not correct to make dynamic routes for this table is like below:

    // Create pages table for dynamic pages
    id | slug | title | page_content 
    


    Then create Page Eloquent model:

    class Page extends Eloquent {
        // ...
    }
    

    Then create Controller for CRUD, you may use a resource controller or a normal controller, for example, normally a PageController:

    class PageController extends BaseController {
    
        // Add methods to add, edit, delete and show pages
    
        // create method to create new pages
        // submit the form to this method
        public function create()
        {
            $inputs = Input::all();
            $page = Page::create(array(...));
        }
    
        // Show a page by slug
        public function show($slug = 'home')
        {
            $page = page::whereSlug($slug)->get();
            return View::make('pages.index')->with('page', $page);
        }
    }
    

    The views/page/index.blade.php view file:

    @extends('layouts.master')
    {{-- Add other parts, i.e. menu --}}
    @section('content')
        {{ $page->page_content }}
    @stop
    

    In defining the routes

    // could be page/{slug} or only slug
    Route::get('/{slug}', array('as' => 'page.show', 'uses' => 'PageController@show'));
    

    By this way we can "Dynamic Routes to controllers from Mysql database".

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: