Frage

I'm just starting with Laravel, so bear with me on this one.

I'm trying to make some kind of CMS with an left content pane and the middle content pane and the right content pane, stuff you all probably know...

Here's an image to describe my goal: enter image description here

My question is: How can i achief this in an nice, good looking way? What do I've got to do in my routes files and how does my controllers looks like?

I've followed the basic tutorials from Laracast, so i know the basics but that's all...

I really hope that somebody can help me ( or point in the right direction ). Because i want to do it the right way at the start, not after 6 months...

War es hilfreich?

Lösung

First of all you have to find a template or create one on your own. I'm going to assume that you will use Bootstrap, so check out this link for a nice dashboard template. You can find many more (free or paid) if you Google it. So you finally found your template! What do you do next? I'll tell you my approach.

Open the template you downloaded and strip off all the contents without touching the positions. For example, in a template the main menu will probably be placed in a div like:

<div class="left-side">

  <ul class="menu">

    <li>...</li>

    <li>...</li>

  </ul>

</div>

Strip off the <ul> and leave just the <div>. There you will place your menu (You'll see later how).

After doing that in all the positions you're gonna have something like this:

<div>

  <div class="left-side"></div>

  <div class="main-content"></div>

</div>

Off course it's gonna be much more than that but you get the idea. This is your master layout. Create a folder in your views e.g. layouts and save it there as master.blade.php.

Now open it again and change it to:

<div>

  <div class="left-side">@include('partials.menu')</div>

  <div class="main-content">@yield('content')</div>

</div>

HOW MENU WORKS

  • Create a new file in your app directory (same level as routes.php) and name it composers.php

  • Open app/start/global.php and at the end of the file place

    require app_path().'/composers.php';

    This way the code in your composers.php file will be triggered in every call.

  • Inside composers.php write the following code:

View::composer('layouts.master', function($view){

  // Just a sample. Get all menu items from the database

  $menus = Menu::all() ;

  // This way $menus will always be available in your master.blade.php

  $view->with('menus', $menus);

});
  • Inside your views folder create a folder partials and inside that folder create menu.blade.php. Now open menu.blade.php and place the following code:

<ul>

@foreach($menus as $menu)

  <li>{{ $menu->title }}</li>    

@endforeach

</ul>

And your menu is ready!

Off course you will need to implement more logic, such us menu highlighting etc. but this will get you started.

HOW MAIN CONTENT WORKS

To have the your content appear in the main area, all your views must extend the master layout. Assuming that you have the following route:

Route::resource('posts', 'PostsController');

a call to e.g. h**p://www.example.com/posts will trigger the index function in PostsController

class PostsController extends BaseController {

  public function index()
  {
    $posts = Post::all();

    return View::make('posts.index', compact('posts'));
  }
}

which then will call the views/posts/index.blade.php file

@extends('layouts.master')

@section('content')

  @foreach($posts as $post)

    <p>{{ $post->content}}</p>

  @endforeach

@stop

pass to it the $menus variable and place it inside master layout.

I hope this will get you started and i also hope that there aren't any typos in my code!

And don't forget that in case of a ready template, will have to

  1. Take care the assets management (.css, .js etc.) yourself

  2. Have your code conform to the template stylesheets

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top