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() {
}
Related
I have the following path inside my / Scope:
$routes->connect('/api/:controller/:action', ['prefix'=>'api'], ['routeClass' => 'DashedRoute']);
And on my src/Controller/UsersController.php i have api_index()
But when i go to the url api/Users/index it says Controller not found, because is asking me to add another UsersController inside a subfolder named Api on Controller folder.
Untill Cakephp 2.x using this behavior worked fine for me, how can i achieve the same behavior on CakePHP 3.x like it was on Cakephp 2.x ?
Thank you very much !
From cakephp book
Prefix Routing
static Cake\Routing\Router::prefix($name, $callback)
Many applications require an administration section where privileged users can make changes. This is often done through a special URL such as /admin/users/edit/5. In CakePHP, prefix routing can be enabled by using the prefix scope method:
use Cake\Routing\Route\DashedRoute;
Router::prefix('admin', function (RouteBuilder $routes) {
// All routes here will be prefixed with `/admin`
// And have the prefix => admin route element added.
$routes->fallbacks(DashedRoute::class);
})
;
Prefixes are mapped to sub-namespaces in your application’s Controller namespace. By having prefixes as separate controllers you can create smaller and simpler controllers. Behavior that is common to the prefixed and non-prefixed controllers can be encapsulated using inheritance, Components, or traits. Using our users example, accessing the URL /admin/users/edit/5 would call the edit() method of our src/Controller/Admin/UsersController.php passing 5 as the first parameter. The view file used would be src/Template/Admin/Users/edit.ctp
You can map the URL /admin to your index() action of pages controller using following route:
Router::prefix('admin', function (RouteBuilder $routes) {
// Because you are in the admin scope,
// you do not need to include the /admin prefix
// or the admin route element.
$routes->connect('/', ['controller' => 'Pages', 'action' => 'index']);
});
https://book.cakephp.org/3/en/development/routing.html#prefix-routing
I have an issue with the admin part of my website using CakePHP 3.2.
This part works really well on wamp in local but when I moved the site to the apache server, it stopped working. I have this error message :
Missing Controller Cake\Routing\Exception\MissingControllerException
Error: DashboardController could not be found. Error: Create the class DashboardController below in file: src/Controller/Admin/DashboardController.php
And this error in the variables :
error : Unserializable object - Cake\Routing\Exception\MissingControllerException. Error: Controller class Dashboard could not be found in /data/vhosts/dev.droplet.ninja/htdev/vendor/cakephp/cakephp/src/Routing/Dispatcher.php, line 79
But the Controller exists at the right path with this content :
<?php
namespace App\Controller\Admin;
use App\Controller\AppController;
class DashboardController extends AppController
{
public function index()
{
}
}
The prefix in my routes.php is :
// Admin namespace
Router::prefix('admin', function ($routes) {
$routes->connect('/', ['controller' => 'Dashboard', 'action' => 'index', 'dashboard']);
$routes->fallbacks('DashedRoute');
});
The routes works fine for the public part of the website but not for this. It seems that it can read the prefix and try to go to the file and even ask me to create the exact same file I already have. The only mistery is why it can't find him.
Also the Controller name is in :
src/Controller/Admin/DashboardController.php
I was looking for the differences between the two apaches settings without finding what can make cakePhp have this behavior.
Do you have any idea ?
Thank you
There's a multitude of reasons why it may not work. In my case, it was because of the old routes cache which I had to clear.
bin/cake cache clear _cake_routes_
You can get the list of cache prefixes by running bin/cake cache list_prefixes.
More info: /3.0/en/console-and-shells/cache.html
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.
I have this blog resource which has the usual CRUD methods.(index, create, store, show, edit, update, destroy).
I have the following route in my routes.php:
Route::resource('blog', 'PostsController');
but I want to restrict all but index and show.
so I have
Route::get('blog', 'PostsController#index');
Route::group(array('before' => 'auth'), function()
{
Route::resource('blog', 'PostsController');
});
which is fine for index but I don't know how to route the show method ? Or is there another way? Instead of routing the resource should I route every URI individually and put the ones I want restricted in my restricted access route?
Cheers
Laravel has a feature that lets you specify filters in the controllers' __construct method using $this->beforeFilter. This function takes a second argument that lets your provide exceptions (or enable the filter only for certain methods). Try using your original routes file and set up your controller like this:
class PostsController extends BaseController {
function __construct() {
// ...
$this->beforeFilter('auth', array('except' => array('index', 'show')));
// ...
}
// ...
See Controller Filters in the Laravel documentation. It's not entirely well-documented, but you can also start a deeper journey into the guts of Laravel from here.
In Laravel 5 you use middleware function instead like this:
$this->middleware('auth', array('except' => array('index', 'show')));
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'));