Cakephp RESTful routing with Plugin - cakephp

Using Cake 2.x I am trying to route RESTful traffic to my Users controller to my UserManagement plugin.
My routes are as follows:
// re-redirect root traffic to login
Router::connect('/', array('plugin'=>'UserManagement','controller' => 'Users', 'action' => 'login'));
CakePlugin::routes();
Router::mapResources(['users']);
Router::parseExtensions();
I also tried:
Router::mapResources(['UserManagement']);
When I call the resful url I get the following error:
users/1.json
{
"code": 404,
"name": "Action UsersController::51() could not be found.",
"message": "Action UsersController::51() could not be found.",
"url": "\/users\/51.json"
}
Thanks in advance.

Following CakePHPs convention over configuration approach, you specify plugins like you do anywhere else, using the plugin syntax, ie prepend the plugin name and separate it with a dot from the controller name
Router::mapResources('UserManagement.Users');
This will of course also require you to use the plugin name in the request URL, ie
/user_management/users/51.json
instead of only
/users/51.json
If you'd wanted to use the latter, but still connect to the plugin, then you could try the prefix option trick, that is, supply the default value of /, which stops the router from using the plugin name as the prefix (not to be confused with actual prefix routing).
Router::mapResources('UserManagement.Users', array(
'prefix' => '/'
));
See also
Cookbook > Plugins > Using a Plugin
Cookbook > Appendencies > Glossary > Plugin Syntax

Related

Josegonzalez/Upload.UploadBehavior could not be found cakephp 3.x

I have a problem configuring Josegonzalez plugin, i get an error as below
Josegonzalez/Upload.UploadBehavior could not be found.
Make sure your plugin Josegonzalez/Upload is in the C:\xampp\htdocs\sunelex\plugins\ directory and was loaded.
Error: Create the class UploadBehavior below in file: C:\xampp\htdocs\sunelex\plugins\Josegonzalez/Upload\src\Model\Behavior\UploadBehavior.php
<?php
namespace Josegonzalez\Upload\Model\Behavior;
use Cake\ORM\Behavior;
class UploadBehavior extends Behavior
{
}
I managed to get the plugin via composer into the plugins folder. I then enabled the plugin in the boostrap.php like Plugin::loadAll(); My table class behavior code looks as follows.
$this->addBehavior('Josegonzalez/Upload.Upload', [
'survey_step3_asbestos_pic' => [
'fields' => [
// if these fields or their defaults exist
// the values will be set.
'dir' => 'photo_dir', // defaults to `dir`
'size' => 'photo_size', // defaults to `size`
'type' => 'photo_type', // defaults to `type`
],
],
]);
Everything is in its place but for some reason I am getting the error above. Could you please help. Thanks.
Using cmd install below command.
composer require josegonzalez/cakephp-upload
You need to enable the plugin your config/bootstrap.php file:
<?php
Plugin::load('Josegonzalez/Upload');
If you are already using Plugin::loadAll();, then this is not necessary.

How to install a plugin manually in CakePHP 3?

I am unable to use Composer and thus have to install CakePDF plugin manually, but following examples from official CakePHP documentation does not seem to work.
So here is installation flow that I have followed:
1.) Copied the plugin to app/plugins/CakePdf
2.) Updated the app's composer.json file, like following:
"autoload": {
"psr-4": {
"CakePdf\\": "./plugins/CakePdf/src",
"CakePdf\\Test\\": "./plugins/CakePdf/tests"
}
},
"autoload-dev": {
"psr-4": {
"App\\Test\\": "tests",
"Cake\\Test\\": "./vendor/cakephp/cakephp/tests"
"CakePdf\\": "./plugins/CakePdf/src",
"CakePdf\\Test\\": "./plugins/CakePdf/tests"
}
}
3.) Loaded the plugin in bootstrap.php:
Plugin::load('CakePdf', ['bootstrap' => true, 'routes' => true, 'autoload' => true]);
4.) Added router extensions:
Router::extensions(['pdf']);
5.) Tried a very simple sample from plugin's doc:
$cakePdf = new CakePdf(array(
'engine' => 'CakePdf.DomPdf',
'pageSize' => 'A4',
'orientation' => 'portrait'
));
$html = '<html><head><body><p>Pdftest</p></body></head></html>';
$rawPdf = $CakePdf->output($html);
However the code breaks at the first line and the following error message is provided:
Class 'App\Controller\CakePdf' not found
I would really appreciate any help or guidance for how a plugin should be installed manually.
If there is any other information that I need to provide, just ask.
You are getting this error because inside vendor/composer/ you can see some autoload_*.php files. These files hold the paths to load your classes. I think no one can safely tell you what to update and where in these files.
So you have two solutions:
1 - Copy composer.json on a local machine and run composer update. Then move the files created inside your app. I would suggest to take a backup before. Most probably the things that you will have to move are:
vendor/
composer.json
composer.lock
2 - Start updating the files inside vendor/composer/autoload_*.php with the paths from the plugin. Most probably you will only need to update the following two files:
vendor/cakephp-plugins.php and vendor/composer/autoload_psr4.php. Personally I wouldn't choose the second solution I am just adding it as an alternative just in case.

How to get $this->webroot work in CakePHP 3.0

I had this piece of code in my services.ctp file which was working fine before in CakePHP 2.3.10.
href="<?php echo $this->webroot . 'intro/services/1'; ?>
I just copied this file into CakePHP 3.0.0 and it's no longer working and throwing the following error message
Error: C:\apache2\htdocs\myprojxxxx\webroot\Helper could not be found.
what's different with this $this->webroot in CakePHP 3.0 ?
Please help!
You need to use this:
href="<?php echo $this->request->webroot . 'intro/services/1'; ?>
This will work with cakephp 3.0
In cakephp 4.x you need to use this:
href="<?php echo $this->Url->webroot.'/intro/services/1'; ?>
This is not how you should have done it in the first place, as such "hard-coded" URLs are very inflexible in comparison to URL arrays, where it's the connected routes that define the generated URLs at a single point in your application, allowing you to easily make changes wihout having to apply modifications throughout the whole application.
That being said, the magic $webroot property is gone (check the migration guide), its value can be retrieved directly via the View::$request object.
You should however use Router::url(), the UrlHelper, or one of the HtmlHelper methods instead:
\Cake\Routing\Router::url(['controller' => 'Intro', 'action' => 'services', 1])
$this->Url->build(['controller' => 'Intro', 'action' => 'services', 1])
$this->Html->link('Title', ['controller' => 'Intro', 'action' => 'services', 1])
See also
Cookbook > Routing > Generating URLs
Cookbook > Views > Helpers > Url
Cookbook > Views > Helpers > Html > Creating Links
Form me in cake 3.9 that work:
<img src="<?php echo $this->Url->build('/img/logo_app.png'); ?>" style="width:250px" />

An internal error has occured

I'm Vaijanath. I'm using cakephp installed on lamp. Now i have created a blog application, but when i run this on localhost/cakephpproject/cakephp/ it is showing an error:
"An Internal Error Has Occured".
And i had changed the "routes.php" in "/app/Config/routes.php" from
"Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));"
to
"Router::connect('/', array('controller' => 'posts', 'action' => 'index'));"
This is an internal error and i'm not able to solve it. Could you please help me in this?
Is your project in your user's public_html dir ?
If so, you must update the three .htaccess files, located in <projectBase>/, <projectBase>/app/ and <projectBase>/app/webroot, and add the following code after each RewriteEngine on statement :
RewriteBase /~<yourUserName>/<projectBase>/
Hope that helped.
In your CakePHP app in the ‘config’ folder change the following setting in the ‘core.php’ file
Configure::write(‘debug’, 0);
Change the ’0′ value to a ’2′ and CakePHP will print all debug errors.
It will display all the errors.. and when the functions works then again change it back to 0.

cakephp WebTechNick paypal plugin(missing controller)

I have downloaded WebTechNick's PayPal plugin and copied the files
into /app/plugins/paypal_ipn (exactly as per the instructions). I have
amended /app/config/routes.php to include the routes for the plugin
(these are copied straight from the installation instructions).
When I access http//:[mysite]/paypal_ipn I am getting a
missing controller error:
Error: PaypalIpnController could not be found.
Error: Create the class PaypalIpnController below in file: app/
controllers/paypal_ipn_controller.php
I'm baffled as I have followed conventions yet this isn't working. I
have other plugins working as expected.
What am I doing wrong?
thanks
i would not use this route (besides, its optional)
Router::connect('/paypal_ipn/:action/*', array('admin' => 'true', 'plugin' => 'paypal_ipn', 'controller' => 'instant_payment_notifications', 'action' => 'index'));
I want my admin stuff to be in /admin/... not having one rouge plugin doing something else
after removing that you should have the following available
site.com/admin/paypal_ipn/paypal_items (shows index like always)
site.com/admin/paypal_ipn/paypal_items/index
site.com/admin/paypal_ipn/paypal_items/view/$id
site.com/admin/paypal_ipn/paypal_items/add
site.com/admin/paypal_ipn/paypal_items/edit/$id
site.com/admin/paypal_ipn/paypal_items/delete/$id
and
site.com/paypal_ipn/instant_payment_notifications/process (need to post to this one)
and
site.com/admin/paypal_ipn/instant_payment_notifications (shows index like always)
site.com/admin/paypal_ipn/instant_payment_notifications/index
site.com/admin/paypal_ipn/instant_payment_notifications/view/$id
site.com/admin/paypal_ipn/instant_payment_notifications/add
site.com/admin/paypal_ipn/instant_payment_notifications/edit/$id
site.com/admin/paypal_ipn/instant_payment_notifications/delete/$id
This is really old, but the selected answer didn't help solve my problem and this thread is the only one that I was able to find.
The issue is that by default, cakephp (as of 2.5.4) does not enable admin prefixing. If (like me) you're not familiar with routing or prefixing, I suggest reading the below links:
Routing: http://book.cakephp.org/2.0/en/development/routing.html
Prefixing: http://book.cakephp.org/2.0/en/development/routing.html#prefix-routing
But, the quick fix to this (assuming your fine with this plugins admin routing process) is to uncomment the admin prefixing line in your core.php. DO NOT TRY ADDING THIS TO routes.php. It won't work. Rather look around line 152 in /app/Config/core.php and change
//Configure::write('Routing.prefixes', array('admin'));
to
Configure::write('Routing.prefixes', array('admin'));
I assume you have added
var $components = array('PluginName.Example'); (adjust the values)
to your (app_)controller?

Resources