Laravel admin area - php

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

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

Related

Laravel link_to_route after namespace change

I was making a basic CRUD app from a tutorial but realised I wanted to mask the feature inside an admin folder.
The feature was a blog management system (index, create, show, delete etc) and this all ran from domain.com/blog.
Since then, I have built a user system and a protected admin area so have decided to move the view files into an admin folder.
To counter this change, I asked on here and was instrcuted to wrap my resource route in this:
Route::group(array('before' => 'is_admin', 'namespace' => 'admin', 'prefix' => 'admin'), function()
Route::resource('blog', 'BlogController');
});
Then move my BlogController into an admin folder in my controller folder, then add a namespace to that controller:
namespace Admin;
and add a backslash before the BaseController.
This line here:
return View::make('admin/blog.index', compact('blogs'));
Was causing errors, so I had to add a backslash before the View::
return \View::make('admin/blog.index', compact('blogs'));
How do I not have to do that for all the classes?
And then once that is okay, my index file contains:
{{ link_to_route('blog.create', 'Add new blog') }}
Which is returning undefined route errors... where am I going wrong? The resource route should be catching these routes etc surely? Seems alot of work to simply make the BlogController work in an admin directory...
This is how namespaces work. You can import namespaces adding:
use View;
and now you will be able to use just View and not \View in other places of your file so the beginning of your file should look like this:
<?php namespace Admin;
use View;
But you will need to add this to each file you moved to namespace Admin;
You could also read How to use objects from other namespaces and how to import namespaces in PHP to understand it a bit better.

Laravel - multiple controllers with same name

I already have a working application and there is a PhoneController.php in controllers folder.
Now I want to add an api for my application so I added api\v1\PhoneController.php
But when I use routing this doesnt work as I want to:
Route::group(array('prefix' => 'api/v1'), function()
{
Route::get('test', 'PhoneController#index');
});
I tried adding 'namespace' => 'api\v1' or api\v1\PhoneController#index but this always picks the wrong PhoneController.
Are there anyways to get it work? I could rename the PhoneController.php but this could confuse me in future, so I am trying to avoid this solution
As described here http://daylerees.com/codebright/controllers
namespace Blog\Controller;
class Article extends \BaseController
{
public function showIndex()
{
return \View::make('index');
}
}
Then add route
Route::post('index', 'Blog\Controller\Article#showIndex');

Best practice for creating administrator interface in Laravel 4

I would like to create an administrator interface for my Laravel project, which is completely separated from the user side.
For example, in Yii framework I can make a module and this will ensure full separation from the user side. Inside a module I can use separate folder structure etc.
This is really a broad question and one answer can't cover everything about best practice for admin controllers or back end management but there are some basic concepts for building an Admin Panel:
// Keep all of your admin routes inside something like this
Route::group(array('prefix'=> 'admin', 'before' => 'auth.admin'), function() {
// Show Dashboard (url: http://yoursite.com/admin)
Route::get('/', array('uses' => 'Admin\\DashBoardController#index', 'as' => 'admin.home'));
// Resource Controller for user management, nested so it needs to be relative
Route::resource('users', 'Admin\\UserController');
});
// Other routes (Non-Admin)
Route::get('login', array('uses' => 'AuthController#showLogin' 'as' => 'login'));
By using a prefix you may separate all admin routes whose url will be prefixed with admin so, if you have a users controller for user management in back end then it's url will be prefixed with admin, i.e. site.com/admin/users. Also using a before filter you may add an authentication for all admin controllers in one place, that means, to access all of your admin controllers user must be logged in and the filter could be something like this:
Route::filter('auth.admin', function($route, $request, $args){
// Check if the user is logged in, if not redirect to login url
if (Auth::guest()) return Redirect::guest('login');
// Check user type admin/general etc
if (Auth::user()->type != 'admin') return Redirect::to('/'); // home
});
For, CRUD (Create, Read, Update, Delete) use a resourceful controller, for example, the UserController in an example of resourceful route declaration.
Use repository classes (Repository Pattern) for decoupling of dependencies, read this article.
Always use a named route, i.e. array('as' => 'routename', 'uses' => 'SomeController#method'), this is an example of naming a route. Named routes are easy to refer, i.e. return Redirect::route('admin.home') will redirect to site.com/admin because we have used admin.home in as to assign the name for that route.
Keep admin controllers in a separate folder and use a namespace for example, Admin\\DashBoardController#index controller should be in app/controllers/admin and your DashBoardController controller should look like this:
<?php namespace Admin;
class DashBoardController extends \BaseController {
public function index()
{
//...
}
}
There are more but it's enough to start with, read articles online and must read the documentation.
If you are familiar with composer you can import in packages (aka modules)
There is a widely available module with multi level interface already called Sentry 2.0:
https://github.com/cartalyst/sentry
You could also make your own if needed if the one I propose is too complex.
There is even a "laravel-ready" version of sentry.
I use the same directory structure that you would like to use on most (if not all) my Laravel projects. Basically, I keep admin views and admin controllers separate from the front-end ones.
Examples:
Controllers:
app/controllers/admin/Admin*Name*Controller.php
app/controllers/site/*Name*Controller.php
Views:
app/views/admin/some_folder/index.blade.php
app/views/site/some_folder/index.blade.php
I would also suggest that you install this laravel project https://github.com/andrewelkins/Laravel-4-Bootstrap-Starter-Site which will give a very good starting on how to organise things in your laravel project. It also has the same folder structure you would like to use.
Good luck.

Unable to change admin prefix in cakephp 1.3

I have built admin of my cakephp site using prefix 'webadmin'. Now I need to change this to something like 'aRRT6nnf'.
I changed the admin prefix in core.php, routes.php and even changes the filenames in views folder but this gives the following error:
Error: The requested address '/aRRT6nnf' was not found on this server.
I made following changes to accomplish this:
//core.php
Configure::write('Routing.prefixes', array('aRRT6nnf'));
//routes.php
Router::connect('/aRRT6nnf', array('controller' => 'dashboard', 'action' => 'index', 'prefix'=>'aRRT6nnf', 'aRRT6nnf'=>true));
Any help would be appreciated.
Have you changed the prefixes on your controller methods
When using admin routing in Cake 1.3 your controller actions need to be prefixed with the route they pertain to for example take route admin your Dashboard controller should be something like this
class DashboardController extends AppController
{
public admin_index() {
}
So in your specific case you would need to change them to
public aRRT6nnf_index() {
}

Laravel: Nesting controllers and making parent controller redirect to children?

This is my first project in Laravel, so play nice with me!
The goal is to create a CMS. Every page will have it's own "slug", so if i name a page This is a test, it's slug will be this-is-a-test. I want to be able to view that page by going to example.com/this-is-a-test.
To do that, i'm guessing i'll have to do something like:
Route::any('(:any)', 'view#index');
And create a controller called View with an index method. All good, right?
The problem is creating the admin area. I'm gonna have a few pages within the area, a few examples are Dashboard, Pages, Settings and Tools. Since all of those are sub-pages in the admin, i figured it would be appropriate to make them nested controllers, right? The only problem is, when i visit /admin, i want to show the dashboard (/admin/dashboard) directly. I would prefer just calling the dashboard controller instead of redirecting to /admin/dashboard from the admin controller. Is that possible?
So, to illustrate what i mean:
example.com/admin -> loads admin.dashboard
example.com/admin/dashboard -> also loads admin.dashboard
Here are all my routes:
Route::get('admin', array('as' => 'admin', 'use' => 'admin.dashboard#index'));
Route::get('admin/dashboard', array('as' => 'admin_dashboard', 'use' =>
Route::any('/', 'view#index'); // Also, should this be below or above the admin routes? This route will show the actual cms pages.'admin.dashboard#index'));
And here is my admin_dashboard controller:
class Admin_Dashboard_Controller extends Base_Controller {
public $restful = true;
public function get_index()
{
return 'in dashboard';
}
}
The view controller just displays a link to the admin page, that works. I just can't figure out what's wrong with the admin routes? When i go to /admin or admin/dashboard i just get a blank page, no 404. If i go to admin/blah or just blabla i get a 404, so i know something is happening, it's just not happening correctly. Am i missing something?
I had misunderstood the naming conventions for controllers.
This is how it was:
controllers
admin
admin_dashboard.php Containing controller Admin_Dashboard_Controller
It's suppose to be:
controllers
admin
dashboard.php Containing controller Admin_Dashboard_Controller
In other words, i shouldn't have prepended "admin" to the beginning of the controllers file name. It all works fine now.
I was actually able to minify the routing code to this as well:
Route::get(array('admin', 'admin/dashboard'), array('as' => 'admin', 'uses' => 'admin.dashboard#index'));

Categories