Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Creating Dynamic Routes to controllers from Mysql database

I have the following table: group_pages in mysql database with page name route name :

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

what I am trying to do is to create dynamic routes in my : routes.php ?

Where if I go to for example: /about it will go to AboutController.php ( which will be created dynamically) is that possible? is it possible to create a dynamic controller file?

I am trying to create dynamic pages routes that links to a controller

example i want to generate this dynamically in my routes.php

Route::controller('about', 'AboutController');  Route::controller('contact', 'ContactController');  Route::controller('blog', 'BlogController'); 
like image 370
user3150060 Avatar asked Apr 17 '14 23:04

user3150060


1 Answers

This is not the right way to create dynamic pages instead, you should use a database and keep all pages in the database. For example:

// 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)->first();         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 

To show pages create a route like this:

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

To access a page, you may require url/link like this:

http://example.com/home http://example.com/about 

This is a rough idea, try to implement something like this.

like image 109
The Alpha Avatar answered Sep 25 '22 13:09

The Alpha