IN cakephp 2.x we can configure multiple prefixes in core.php file. like
Configure::write('Routing.prefixes', array('admin','blogger'));
But in cake php 3.X the structure of directory has been changed. There is no core.php file, So how we can configure multiple prefix in cakephp 3.x
Route prefixes get defined in the config/routes.php file in CakePHP 3 using Router::prefix(). So in your case you'd want something like this:-
Router::prefix('admin', function ($routes) {
$routes->fallbacks('DashedRoute');
});
Router::prefix('blogger', function ($routes) {
$routes->fallbacks('DashedRoute');
});
One other change you need to take into account from CakePHP 2.x to 3.x is that prefixes are mapped to sub-namespaces in your application’s Controller namespace. So for example, if you have a Pages model non-prefixed actions would go in PagesController and any actions prefixed admin would reside in Admin/PagesController.
Checkout the documentation on routes for more details.
Related
I have Cakephp 3 application and i want to load this plugin and the event listener below it just for admin prefix. ( not the entire project ) .
how can i achive this?
this is my
config/bootstrap.php
where i load this plugin . (I load these at end of the file )
...
Plugin::load('Josegonzalez/Upload');
\Cake\Event\EventManager::instance()->on(new \App\Controller\Event\AppEventListener());
i want these to line only run on admin prefix
You can't conditionally run code based upon routing inside boostrap.php as the routing configuration for you app hasn't been loaded yet.
You'll have to do it old school style and use $_SERVER['REQUEST_URI'].
$url = $_SERVER['REQUEST_URI'];
$admin = '/admin/';
if(substr($url, 0, strlen($admin)) === $admin) {
Plugin::load('Josegonzalez/Upload');
\Cake\Event\EventManager::instance()->on(new \App\Controller\Event\AppEventListener());
}
Updated
Your plugin should have it's own bootstrap file. Which contains
Upload/config/bootstrap.php:
\Cake\Event\EventManager::instance()->on(new \App\Controller\Event\AppEventListener());
The app's bootstrap then tells CakePHP to include the bootstrap when loading the plugin.
App/config/bootstrap.php:
Plugin::load('Josegonzalez/Upload', ['bootstrap' => true]);
You shouldn't conditionally load your plugin.
I think you are searching for authentication on requests to your plugin. You want to limit use of your plugin only to authorized administrators. That is not a plugin issue. It is an authentication issue and you should be using the Authentication component in your plugin to verify the request is valid, and that the current user is an administrator.
Quoting cakephp plugin docs https://book.cakephp.org/3.0/en/plugins.html#plugin-routes
You can also load plugin routes in your application’s routes list. Doing this provides you more control on how plugin routes are loaded and allows you to wrap plugin routes in additional scopes or prefixes.
So you can do
Router::scope('/', function ($routes) {
// Connect other routes.
$routes->scope('/admin', function ($routes) {
$routes->loadPlugin('Josegonzalez/Upload');
});
});
For your event listener, load it inside plugin bootstrap configuration
// inside plugin bootstra.php
\Cake\Event\EventManager::instance()->on(new
\App\Controller\Event\AppEventListener());
Can anyone tell me what is the official way to create CRUD for admin back-end?
In CakePHP 2 the baked code was extended with 'admin_' before the function names and for the view files.
In CakePHP it seems I can't find any direct information on how it's done anymore. The bake console doesn't ask for admin anymore.
In this topic: https://github.com/cakephp/bake/issues/28 I see that they mention to use the --prefix extension but then the controller is placed in a separate /Admin folder while the CRUD functions keep having their normal name. And in some parts of the cookbook () I still see they mention functions like admin_view.
So can anyone tell me what is the official 'Cake'-way to do this from 3.2 on?
If you want to create Controller using cake bake. You can do this with below command:
bin/cake bake controller --prefix admin users
For view:
bin/cake bake template --prefix admin users
It creates the admin folder in the template directory, Then it creates the folder for users, then it includes the files. for admin prefix folder structure like template/admin/users/index.ctp
See official cookbook documentation
Also In your config/routes.php add this:
Router::prefix('admin', function ($routes) {
$routes->connect('/', ['controller' => 'Users', 'action' => 'index']);
$routes->extensions(['json', 'xml']);
// All routes here will be prefixed with `/admin`
// And have the prefix => admin route element added.
$routes->fallbacks('DashedRoute');
});
That's how things work now in CakePHP 3, prefixed methods are gone, prefixes now do generate separate controllers in sub-namespaces, for smaller/simpler controllers, and for proper separation, not only on controller level, but also on template level, where the templates are expected to be placed in separate folders accordingly.
The admin_view example you are referring to is just an example that should show how to manually set a custom layout for specific actions, it has nothing to do with prefix routing.
So, if you want to use prefix routing, then the "official" way is to use the --prefix option.
See also
Cookbook > Routing > Prefix Routing
Cookbook > Bake Console > Code Generation with Bake
Below is the bake command to bake all prefix controller and templates for users table
cake bake all users --prefix admin
And here is the route code to make it working:-
Router::prefix('admin', function ($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' => 'Users', 'action' => 'index']);
$routes->extensions(['json', 'xml']);
// All routes here will be prefixed with `/admin`
//$routes->connect('/admin', ['controller' => 'Order', 'action' => 'index']); // call other controller like this
// And have the prefix => admin route element added.
$routes->fallbacks('DashedRoute');
});
This will work for me hope it will work for you :)
I've set up my Raspberry Pi 2 with a nginx, php, mysql environment and installed cakePHP with composer.
Now the cakePHP landing page looks weird. I'm assuming that this issue is related to my nginx vhost configuration. (Conf with vhost works, CSS files loading)
The question is: is it possible to use cake without vhost conf?
The route is set to the respective folder:
$routes->connect('/test/', ['controller' => 'Pages', 'action' => 'display', 'home']);
With best regards,
Phil
Solved the problem by adding these lines into my /etc/nginx/sites-enabled/default conf-file:
location /test/ {
alias /usr/share/nginx/www/test/webroot/;
}
I am currently developing an Restfull API for my website.
I decided to develop it as a plugin.
I am using a custom class that extends BasicAuthentification. It allows me to check the client-app credential, in order to limit the API use to only approved developers.
This file works perfectly when added in the CakePHP CORE : Cake/Controller/Component/Auth/DeviceAuthentification.php
Since I am developing a Plugin, I would like everything to be inside the same directory.
Therefore in my plugin directory called MyApi, I added my custom class in the following path :
MyApi/Controller/Auth/DeviceAuthentification.php
In order to load it, in my Plugin's controller MyApiAppController, I added the following code :
public $components = array(
'Auth' => array(
'authenticate' => 'Device', // I also tried MyApi.Device
'sessionKey' => false
)
);
It does not load, the error is the following :
Authentication adapter "Device" was not found.
Anybody has an idea ?
Well, after having a look in the core file AuthComponent, it seems that you need to have the following path :
MyApi/Controller/Component/Auth/DeviceAuthentification.php
instead of
MyApi/Controller/Auth/DeviceAuthentification.php
Therefore, whenever you are working in the Plugin directory, you need to add the directory Component
I have a problem with CakePHP (1.3) project, i need to create projects inside the same domain/hosting of other project, like this:
app
cake
plugins
templates
vendors
ADMINONE (Cake project files here)
ADMINTWO (another Cake project files here)
.htaccess
index.php
README
The problem is, when I put the CakePHP code inside ADMINONE or ADMINTWO to create the other cake projects, if I put the url http://domain.com/ADMINONE/, the URL redirects to http://domain.com or the cake read the /ADMINONE as a controller name.
How can I redirect this URL to the directory ignoring the primary directory cake's redirect?
Dont forget to change your index.php inside the webroot folder. In there you can set up the
APP_DIR and the ROOT if need be.