10

I come from a Codeigniter background. At the moment I am building a CMS in Laravel.

What I would like to know is how can I separate the backend and frontend in Laravel?

In Codeigniter i use to make two controller Admin_Controller and Front_Controller.

Article extends Admin_Controller
Article extends Front_Controller

and the file structure looked like this

controller
--admin
---user
---blog
---news
--user 
--blog
--news

for admin controller I make separate folder and front end controller remain in root of controller folder.

Should I use the same logic in Laravel or is there a better way to do it?

2
  • 2
    With Laravel the 'how do I architect'-type questions are very much up to you. I think the system you proposed, that you already know and use, is perfectly fine. Commented Feb 19, 2014 at 14:00
  • 1
    Your question isn't really about separating front and back ends, but about how to organize your files. Choose whatever file structure is easiest to work with. Laravel doesn't care which files are in which folders so long as they can be properly autoloaded. Commented Feb 19, 2014 at 14:58

2 Answers 2

35

If you want to create thinks like Taylor Otwell and 'the core' is trying to teach people do things in Laravel, this is a good start:

Your files could be organized as

├── app
│   ├── ZIP
│   │   ├── Controllers
│   │   │   ├── Admin
│   │   │   │   ├── Base.php <--- your base controller
│   │   │   │   ├── User.php
│   │   │   │   ├── Blog.php
│   │   │   │   ├── News.php
│   │   │   ├── Front
│   │   │   │   ├── Base.php <--- your base controller
│   │   │   │   ├── User.php
│   │   │   │   ├── Blog.php
│   │   │   │   ├── News.php

Configure a PSR-0 or PSR-4 (better) to autoload your classes:

"psr-0": {
    "ZIP": "app/"
},

Create namespaces to all tour classes, according to your source tree:

<?php namespace ZIP\Controllers\Admin

class User extends Base {

}


<?php namespace ZIP\Controllers\Front

class Blog extends Base {

}

And create your base controllers

<?php namespace ZIP\Controllers\Admin

use Controller;

class Base extends Controller {

}
Sign up to request clarification or add additional context in comments.

7 Comments

I don't understand why someone downvoted on this. This is perfectly valid answer to this question. App structure here is superb here!
Looks like we have some haters around... :)
yup i didnt expect that even this answer an get downvote ,one up from me
Does the name ZIP have any special meaning here?
@AntonioCarlosRibeiro What about separate users, users cookies and all that stuff?
|
2

You can certainly do it the two controllers way or if you like even more separation (and a more 'laravel' way), write your front end and backend as separate packages (previously called bundles in Laravel 3).

They basically behave like standalone applications within your main app. They can have their own routes, models, controllers etc. You can also write 'core code' at the main application level which can be shared across the packages.

If you are moving to Laravel as you want to learn a new framework, then you should definitely try and get a handle on packages - very powerful.

If you are being 'made' to move to Laravel, or have some time pressure, just do it as you have normally done. Laravel is flexible and will be fine either way you do it.

For more info, see the docs.

Laravel current version (4 at time of writing) - http://laravel.com/docs/packages

Laravel 3 - http://three.laravel.com/docs/bundles

1 Comment

@gaz-edge : Is there any separation like frontend and backend like Yii advanced template? I mean any link to already implemented repository on githhub or elsewhere?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.