Lovelock Lovelock - 24 days ago 19
PHP Question

Laravel admin area

working on a basic template for all future projects and decided to use Laravel.

The aim is just to create a starting point for any websites I make in the future (basic blog, admin area + user login system).

I was following a tutorial on the CRUD basics and made a simple blog management app. However I wish to put this into an /admin area.

Before, I had this in my route:

Route::resource('blog', 'BlogController');


Which then allowed me to simply use the functions in my BlogController on the domain.com/blog URL.

This is all working, but I want to mask this behind an admin area. I thought I could just move the blog folder with all my blog views in, into a admin folder but hitting route not defined errors.

The views folder is:

- Views

- admin

- blog
- edit.blade.php
- index.blade.php
- new.blade.php
- show.blade.php

- home.blade.php


Before, my blog folder was simply in the views folder itself. What do I need to change to get domain.com/admin/blog working the same as it did before?

Using Laravel 4.2

Answer

You could group controllers together to be inside admin folder and this will be a lot easier for managing files.

Laravel 5 Example

routes.php

Route::group(array('namespace' => 'admin', 'prefix' => 'admin'), function() {
  Route::resource('blog', 'BlogController');
});

and then you can create BlogController.php inside admin folder

/app/Http/Controllers/admin/BlogController.php

the example of BlogController.php file

<?php namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;

class BlogController extends Controller {

  public function index()
  {
    echo "admin/blog/index";
  }
}

and then you can call http://localhost:8000/admin/blog

Laravel 4 Example

routes.php

Route::group(array('namespace' => 'admin', 'prefix' => 'admin'), function() {
  Route::resource('blog', 'BlogController');
});

and then you can create BlogController.php inside admin folder

/app/controllers/admin/BlogController.php

the example of BlogController.php file

<?php namespace Admin;

class BlogController extends \BaseController {

  public function index()
  {
    echo "admin/blog/index";
  }
}

and then you can call http://localhost:8000/admin/blog

Comments