Advertisement
  1. Code
  2. PHP
  3. Laravel

Build Your First Admin Bundle for Laravel

Scroll to top

It's hard to deny the fact that the PHP community is excited for Laravel 4. Among other things, the framework leverages the power of Composer, which means it's able to utilize any package or script from Packagist.

In the meantime, Laravel offers "Bundles", which allow us to modularize code for use in future projects. The bundle directory is full of excellent scripts and packages that you can use in your applications. In this lesson, I'll show you how to build one from scratch!


Wait, What's a Bundle?

Bundles offer an easy way to group related code. If you're familiar with CodeIgniter, bundles are quite similar to "Sparks". This is apparent when you take a look at the folder structure.

Folder StructureFolder StructureFolder Structure

Creating a bundle is fairly simple. To illustrate the process, we'll build an admin panel boilerplate that we can use within future projects. Firstly, we need to create an 'admin' directory within our 'bundles' folder. Try to replicate the folder structure from the image above.

Before we begin adding anything to our bundle, we need to first register it with Laravel. This is done in your application's bundles.php file. Once you open this file, you should see an array being returned; we simply need to add our bundle and define a handle. This will become the URI in which we access our admin panel.

1
'admin' => array('handles' => 'admin')

Here, I've named mine, "admin," but feel free to call yours whatever you wish.

Once we've got that setup, we need to create a start.php file. Here, we're going to set up a few things, such as our namespaces. If you're not bothered by this, then you don't actually need a start file for your bundle to work, as expected.

Laravel's autoloader class allows us to do a couple of things: map our base controller, and autoload namespaces.

1
Autoloader::map(array(
2
    'Admin_Base_Controller' => Bundle::path('admin').'controllers/base.php',
3
));
4
5
Autoloader::namespaces(array(
6
    'Admin\Models' => Bundle::path('admin').'models',
7
    'Admin\Libraries' => Bundle::path('admin').'libraries',
8
));

Namespacing will ensure that we don't conflict with any other models or libraries already included in our application. You'll notice that we haven't opted to not namespace our controllers to make things a little easier.


Publishing Assets

For the admin panel, we'll take advantage of Twitter's Bootstrap, so go grab a copy. We can pop this into a public folder inside our bundle in order to publish to our application later on.

When you're ready to publish them, just run the following command through artisan.

1
php artisan bundle:publish admin

This will copy the folder structure and files to the bundles directory in our public folder, within the root of the Laravel installation. We can then use this in our bundle's base controller.


Setting up the Base Controller

It's always a smart idea to setup a base controller, and extend from there. Here, we can setup restful controllers, define the layout, and include any assets. We just need to call this file, base.php, and pop it into our controller's directory.

Firstly, let's get some housekeeping out of the way. We'll of course want to use Laravel's restful controllers.

1
public $restful = true;

And we'll specify a layout that we'll create shortly. If you're not used to controller layouts, then you're in for a treat.

1
public $layout = 'admin::layouts.main';

The bundle name, followed by two colons, is a paradigm in Laravel we'll be seeing more of in the future, so keep an eye out.

When handling assets within our bundle, we can do things as expected and specify the path from the root of the public folder. Thankfully, Laravel is there to make our lives easier. In our construct, we need to specify the bundle, before adding to our asset containers.

1
Asset::container('header')->bundle('admin');
2
Asset::container('footer')->bundle('admin');

If you're unfamiliar with asset containers, don't worry; they're merely sections of a page where you want to house your assets. Here, we'll be including stylesheets in the header, and scripts in the footer.

Now, with that out of the way, we can include our bootstrap styles and scripts easily. Our completed base controller should look similar to:

1
class Admin_Base_Controller extends Controller {
2
3
    public $restful = true;
4
    public $layout = 'admin::layouts.main';
5
6
    public function __construct(){
7
8
        parent::__construct();
9
10
        Asset::container('header')->bundle('admin');
11
        Asset::container('header')->add('bootstrap', 'css/bootstrap.min.css');
12
13
        Asset::container('footer')->bundle('admin');
14
        Asset::container('footer')->add('jquery', 'http://code.jquery.com/jquery-latest.min.js');
15
        Asset::container('footer')->add('bootstrapjs', 'js/bootstrap.min.js');
16
17
    }
18
19
    /**

20
     * Catch-all method for requests that can't be matched.

21
     *

22
     * @param  string    $method

23
     * @param  array     $parameters

24
     * @return Response

25
     */
26
    public function __call($method, $parameters){
27
        return Response::error('404');
28
    }
29
30
}

We've also brought across the catch-all request from the application's base controller to return a 404 response, should a page not be found.

Before we do anything else, let's make the file for that layout, views/layout/main.blade.php, so we don't encounter any errors later on.


Securing the Bundle

As we're building an admin panel, we're going to want to keep people out. Thankfully, we can use Laravel's built in Auth class to accomplish this with ease..

First, we need to create our table; I'm going to be using 'admins' as my table name, but you can change it, if you wish. Artisan will generate a migration, and pop it into our bundle's migrations directory. Just run the following in the command line.

1
php artisan migrate:make admin::create_admins_table

Building the Schema

If you're unfamiliar with the schema builder, I recommend that you take a glance at the documentation. We're going to include a few columns:

  • id - This will auto-increment and become our primary key
  • name
  • username
  • password
  • email
  • role - We won't be taking advantage of this today, but it will allow you to extend the bundle later on

We'll also include the default timestamps, in order to follow best practices.

1
/**

2
 * Make changes to the database.

3
 *

4
 * @return void

5
 */
6
public function up()
7
{
8
    Schema::create('admins', function($table)
9
    {
10
        $table->increments('id');
11
        $table->string('name', 200);
12
        $table->string('username', 32)->unique();
13
        $table->string('password', 64);
14
        $table->string('email', 320)->unique();
15
        $table->string('role', 32);
16
        $table->timestamps();
17
    });
18
}
19
20
/**

21
 * Revert the changes to the database.

22
 *

23
 * @return void

24
 */
25
public function down()
26
{
27
    Schema::drop('admins');
28
}

Now that we've got our database structure in place, we need to create an associated model for the table. This process is essentially identical to how we might accomplish this in our main application. We create the file and model, based on the singular form of our table name - but we do need to ensure that we namespace correctly.

1
namespace Admin\Models;
2
use \Laravel\Database\Eloquent\Model as Eloquent;
3
4
class Admin extends Eloquent {
5
}

Above, we've ensured that we're using the namespace that we defined in start.php. Also, so we can reference Eloquent correctly, we create an alias.

Extending Auth

To keep our bundle entirely self contained, we'll need to extend auth. This will allow us to define a table just to login to our admin panel, and not interfere with the main application.

Before we create our custom driver, we'll create a configuration file, where you can choose if you'd like to use the username or email columns from the database table.

1
return array(
2
    'username' => 'username',
3
    'password' => 'password',
4
);

If you want to alter the columns that we'll be using, simply adjust the values here.

We next need to create the driver. Let's call it, "AdminAuth," and include it in our libraries folder. Since we're extending Auth, we only need to overwrite a couple of methods to get everything working, as we intended.

1
2
namespace Admin\Libraries;
3
use Admin\Models\Admin as Admin, Laravel\Auth\Drivers\Eloquent as Eloquent, Laravel\Hash, Laravel\Config;
4
5
class AdminAuth extends Eloquent {
6
7
/**

8
 * Get the current user of the application.

9
 *

10
 * If the user is a guest, null should be returned.

11
 *

12
 * @param  int|object  $token

13
 * @return mixed|null

14
 */
15
public function retrieve($token)
16
{
17
    // We return an object here either if the passed token is an integer (ID)

18
    // or if we are passed a model object of the correct type

19
    if (filter_var($token, FILTER_VALIDATE_INT) !== false)
20
    {
21
        return $this->model()->find($token);
22
    }
23
    else if (get_class($token) == new Admin)
24
    {
25
        return $token;
26
    }
27
}
28
29
/**

30
 * Attempt to log a user into the application.

31
 *

32
 * @param  array $arguments

33
 * @return void

34
 */
35
public function attempt($arguments = array())
36
{
37
    $user = $this->model()->where(function($query) use($arguments)
38
    {
39
        $username = Config::get('admin::auth.username');
40
41
        $query->where($username, '=', $arguments['username']);
42
43
        foreach(array_except($arguments, array('username', 'password', 'remember')) as $column => $val)
44
        {
45
            $query->where($column, '=', $val);
46
        }
47
    })->first();
48
49
    // If the credentials match what is in the database, we will just

50
    // log the user into the application and remember them if asked.

51
    $password = $arguments['password'];
52
53
    $password_field = Config::get('admin::auth.password', 'password');
54
55
    if ( ! is_null($user) and Hash::check($password, $user->{$password_field}))
56
    {
57
        return $this->login($user->get_key(), array_get($arguments, 'remember'));
58
    }
59
60
    return false;
61
}
62
63
protected function model(){
64
    return new Admin;
65
}
66
67
}

Now that we've created the driver, we need to let Laravel know. We can use Auth's extend method to do this in our start.php file.

1
Auth::extend('adminauth', function() {
2
    return new Admin\Libraries\AdminAuth;
3
});

One final thing that we need to do is configure Auth to use this at runtime. We can do this in our base controller's constructor with the following.

1
Config::set('auth.driver', 'adminauth');

Routes & Controllers

Before we can route to anything, we need to create a controller. Let's create our dashboard controller, which is what we'll see after logging in.

As we'll want this to show up at the root of our bundle (i.e. the handle we defined earlier), we'll need to call this home.php. Laravel uses the 'home' keyword to establish what you want to show up at the root of your application or bundle.

Extend your base controller, and create an index view. For now, simply return 'Hello World' so we can ensure that everything is working okay.

1
class Admin_Home_Controller extends Admin_Base_Controller {
2
3
    public function get_index(){
4
        return 'Hello World';
5
    }
6
7
}

Now that our controller is setup, we can route to it. Create a routes.php within your bundle, if you haven't already. Similar to our main application, each bundle can have its own routes file that works identically.

1
Route::controller(array(
2
    'admin::home',
3
));

Here, I've registered the home controller, which Laravel will automatically assign to /. Later , we'll add our login controller to the array.

If you head to /admin (or whatever handle you defined earlier) in your browser, then you should see 'Hello World'.


Building the Login Form

Let's create the login controller, however, rather than extending the base controller, we'll instead extend Laravel's main controller. The reason behind this decision will become apparent shortly.

Because we're not extending, we need to set a couple of things up before beginning - namely restful layouts, the correct auth driver, and our assets.

1
class Admin_Login_Controller extends Controller {
2
3
    public $restful = true;
4
5
    public function __construct(){
6
7
        parent::__construct();
8
9
        Config::set('auth.driver', 'adminauth');
10
11
        Asset::container('header')->bundle('admin');
12
        Asset::container('header')->add('bootstrap', 'css/bootstrap.min.css');
13
14
    }
15
16
}

Let's also create our view. We're going to be using Blade - Laravel's templating engine - to speed things up a bit. Within your bundles views directory, create a 'login' directory and an 'index.blade.php' file within it.

We'll pop in a standard HTML page structure and echo the assets.

1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
    <meta charset="utf-8">
5
    <title>Login</title>
6
    {{Asset::container('header')->styles()}}
7
    <!--[if lt IE 9]>

8
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>

9
    <![endif]-->
10
</head>
11
<body>
12
13
</body>
14
</html>

Now, let's make sure that the view is being created in the controller. As we're using restful controllers, we can take advantage of the 'get' verb in our method.

1
public function get_index(){
2
    return View::make('admin::login.index');
3
}

Awesome! We're now good to start building our form, which we can create with the Form class.

1
{{Form::open()}}
2
3
{{Form::label('username', 'Username')}}
4
{{Form::text('username')}}
5
6
{{Form::label('password', 'Password')}}
7
{{Form::password('password')}}
8
9
{{Form::submit('Login', array('class' => 'btn btn-success'))}}
10
11
{{Form::token()}}
12
{{Form::close()}}
Login FormLogin FormLogin Form

Above, we created a form that will post to itself (exactly what we want), along with various form elements and labels to go with it. The next step is to process the form.

As we're posting the form to itself and using restful controllers, we just need to create the post_index method and use this to process our login. If you've never used Auth before, then go and have a peek at the documentation before moving on.

1
public function post_index(){
2
3
    $creds = array(
4
        'username' => Input::get('username'),
5
        'password' => Input::get('password'),
6
    );
7
8
    if (Auth::attempt($creds)) {
9
        return Redirect::to(URL::to_action('admin::home@index'));
10
    } else {
11
        return Redirect::back()->with('error', true);
12
    }
13
14
}

If the credentials are correct, the user will be redirected to the dashboard. Otherwise, they'll be redirected back with an error that we can check for in the login view. As this is just session data, and not validation errors, we only need to implement a simple check.

1
@if(Session::get('error'))
2
    Sorry, your username or password was incorrect.
3
@endif

We'll also need to log users out; so let's create a get_logout method, and add the following. This will log users out, and then redirect them when visiting /admin/login/logout.

1
public function get_logout(){
2
    Auth::logout();
3
    return Redirect::to(URL::to_action('admin::home@index'));
4
}

The last thing we should do is add the login controller to our routes file.

1
Route::controller(array(
2
    'admin::home',
3
    'admin::login',
4
));

Filtering routes

To stop people from bypassing our login screen, we need to filter our routes to determine if they're authorized users. We can create the filter in our routes.php, and attach it to our base controller, to filter before the route is displayed.

1
Route::filter('auth', function() {
2
    if (Auth::guest()) return Redirect::to(URL::to_action('admin::login'));
3
});

At this point, all that's left to do is call this in our base controller's constructor. If we extended our login controller from our base, then we'd have an infinite loop that would eventually time out.

1
$this->filter('before', 'auth');

Setting up the Views

Earlier, we created our main.blade.php layout; now, we're going to do something with it. Let's get an HTML page and our assets being brought in.

1
<!DOCTYPE html>
2
<html lang="en">
3
  <head>
4
    <meta charset="utf-8">
5
    <title>{{$title}}</title>
6
    {{Asset::container('header')->styles()}}
7
    <!--[if lt IE 9]>

8
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>

9
    <![endif]-->
10
  </head>
11
  <body>
12
    <div class="container">
13
        {{$content}}
14
    </div>
15
  {{Asset::container('footer')->scripts()}}
16
  </body>
17
  </html>

You'll notice that I've also echoed out a couple of variables: $title and $content. We'll be able to use magic methods from our controller to pass data through to these. I've also popped $content inside the container div that Bootstrap will provide the styling for.

Next, let's create the view for our dashboard. As we'll be nesting this, we only need to put the content we want to put into our container.

1
<h1>Hello</h1>
2
<p class="lead">This is our dashboard view</p>

Save this as index.blade.php within the views/dashboard directory inside of your bundle.

We now need to set our controller to take advantage of the layout and view files that we just created. Within the get_index method that we created earlier, add the following.

1
$this->layout->title = 'Dashboard';
2
$this->layout->nest('content', 'admin::dashboard.index');

title is a magic method that we can then echo out as a variable in our layout. By using nest, we're able to include a view inside the layout straight from our controller.


Creating a Task

In order to speed things up, Laravel provides us with an easy way to execute code from the command line. These are called "Tasks"; it's a good idea to create one to add a new user to the database easily.

We simply need to ensure that the file takes on the name of our task, and pop it into our bundle's tasks directory. I'm going to call this setup.php, as we'll use it just after installing the bundle.

1
2
use Laravel\CLI\Command as Command;
3
use Admin\Models\Admin as Admin;
4
5
class Admin_Setup_Task {
6
7
public function run($arguments){
8
9
    if(empty($arguments) || count($arguments) < 5){
10
        die("Error: Please enter first name, last name, username, email address and password\n");
11
    }
12
13
    Command::run(array('bundle:publish', 'admin'));
14
15
    $role = (!isset($arguments[5])) ? 'admin' : $arguments[5];
16
17
    $data = array(
18
        'name' => $arguments[0].' '.$arguments[1],
19
        'username' => $arguments[2],
20
        'email' => $arguments[3],
21
        'password' => Hash::make($arguments[4]),
22
        'role' => $role,
23
    );
24
25
    $user = Admin::create($data);
26
27
    echo ($user) ? 'Admin created successfully!' : 'Error creating admin!';
28
29
}
30
31
}

Laravel will pass through an array of arguments; we can count these to ensure that we're getting exactly what we want. If not, we'll echo out an error. You'll also notice that we're using the Command class to run bundle:publish. This will allow you to run any command line task built into Laravel inside your application or bundle.

The main thing this task does is grab the arguments passed through to it, hash the password, and insert a new admin into the Admins table. To run this, we need to use the following in the command line.

1
php artisan admin::setup firstname lastname username email@address.com password

What Now?

In this tutorial, we created an boilerplate admin panel that is quite easy to extend. For example, the roles column that we created could allow you to limit what your clients are able to see.

A bundle can be anything from an admin panel, like we built today, to Markdown parsers - or even the entire Zend Framework (I'm not kidding). Everything that we covered here will set you on your way to writing awesome Laravel bundles, which can be published to Laravel's bundle directory.

Learn more about creating Laravel bundles here on Nettuts+.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.