I am trying to get my head around the documentation for installation and use of this Uploader plugin for CakePHP 2.x which deals with uploading of files. I have a few questions I hope somebody could help me with. There's quite a good thread on the topic, Installing Uploader Plugin for CakePHP 2.x, but I wanted to clear some things out which are not that clear in the thread (to me at least).
Btw the authors page: http://milesj.me/code/cakephp/uploader
Is it right that if you want to save manually (without the behaviour), the installation is as follows:
CakePlugin::load('Uploader'); // THIS GOES INTO THE BOOTSTRAP.PHP FILE PER THE REFERENCED QUESTION ANSWER
App::import('Vendor', 'Uploader.Uploader'); // THIS GOES INTO CONTROLLER PER THE REFERENCED QUESTION ANSWER
$this->Uploader = new Uploader(); // THIS GOES INTO CONTROLLER PER THE REFERENCED QUESTION ANSWER
Is that right?
BUT, if you are using the behaviour instead you don't do ANY of that above for installation, instead you do ONLY in model:
public $actsAs = array('Uploader.Attachment');
Is that right?
Is chapter 2 of the documentation only applicable for when using the plugin manually? Otherwise, you configure the model behaviour per chapter 3 only in the model.
Final question: So if I understand this right (I am still a little new to CakePHP), if I am using the behaviour to do the save automatically, whenever I upload a file to that model, it will automatically save the file in the right folder without me having to do anything and put a reference in the $data variable which is pointing to the saving location?
Thanks in advance!
I had such trouble getting this to work and understanding the whole plugin and behaviour thing so I thought it would be worth sharing quickly what I have learnt in a short video on how to install, implement and use the Uploader plugin through the model behaviour.
http://www.youtube.com/watch?v=lMNUOz8wqzE
Hope you find it helpful. To answer my questions above, I basically only have to:
Include the CakePlugin::load('Uploader'); in the bootstrap.php
file which can be found under App/Config/
Call the behaviour inside my model as per public $actsAs =
array('Uploader.Attachment'); but with the relevant options
configured
Then just save to the model, through my controller, and in the model I have included the $actsAs variable
The beforeSave callback function which has been defined in the Attachment behaviour in the Plugin will take care of the rest.
Uploading file seems to be such a mundane tasks so I suppose it is very appropriate to do it through a behaviour, and I don't want to write my own behaviour given my own beginners level so it's good that Miles has, particularly since he's an experienced developer. After reading up, uploading files using controller code is not the way to go, using expert developers plugin's probably is THE way to go.
Related
Is there any way to auto-generate Swagger documentation (or similar) for a Nancy service?
I found Nancy.Swagger, but there's no information on how to use it and the demo application doesn't seem to demonstrate generating documentation (if it does, it's not obvious).
Any help would be appreciated. Thanks!
In my current project I've been looking a lot into this problem. I used both nancy.swagger and nancy.swagger.attributes.
I quickly discarded Nancy.swagger, because for me personally it doesn't sound right that you have to create a pure documentation class for each nancy module. The attributes solution was a bit "cleaner" - at least codebase and documentation were in one place. But very fast this became unmaintainable. Module code is unreadable because of many attributes. Nothing is generated automatically: you have to put path, all parameters, even http method as an attribute. This is a huge effort duplication. Problems came very fast, a few examples:
I changed POST to PUT in Nancy and forgot to update [Method] attribute.
I added a parameter but not the attribute for it.
I changed parameter from path to query and didn't update the attribute.
It's too easy to forget to update the attributes (let alone documentation module solution), which leads to discrepancies between your documentation and actual code base. Our UI team is in another country and they had some trouble using the APIs because docu just wasn't up-to-date.
My solution? Don't mix code and documentation. Generating docu from code (like Swashbuckle does) IS ok, but actually writing docu in code and try to dublicate the code in docu is NOT. It's not better than writing it in a Word document for your clients.
If you want Swagger docu, just do it the Swagger way.
- Spend some time with Swagger.Editor and really author your API in
YAML. It looks all-text and hard, but once you get used to it, it's
not.
- Spend some time with Swagger.Codegen and adapt it (it already does a fair job for generating Nancy server code and with a few
adjustments to moustache templates it was just what I needed).
- Automate your process: write a couple of batches to generate your modules and models from yaml and copy them to your repository.
Benefits? Quite a few:
-
Your YAML definition is now the single truth of your REST contract.
If somewhere something is defferent, it's wrong.
Nancy server code is auto-generated
Client code-bases are auto-generated (in our case it's android, ios and angular)
So whenever I change something in REST contract, all codebases are regenerated and added to projects in one batch. I just have to tell the teams something was updated. They don't have to look through some documents and search for it. They just have their code regenerated and probably see some compile errors, in case of breaking changes.
Do I still use nancy.swagger(.annotations)?
Yes, I do use it in another project, which has just one endpoint with a couple of methods. They don't change often. It's not worth the effort to set up everything, I have my swagger docu fast up and running. But if your project is big, API is changing, and you have multiple code-bases depending on your API, my advice is to invest some time into a real swagger setup.
I am quoting the author answer here from https://github.com/khellang/Nancy.Swagger/issues/59
The installation should be really simple, just pull down the NuGet package, add metadata modules to describe your routes, and hit /api-docs. That should get you the JSON. If you want to add swagger-ui as well, you have to add that manually right now.
No. Not in an automated. https://github.com/yahehe/Nancy.Swagger needs lots of manually created metadata.
There is a nice article here: http://www.c-sharpcorner.com/article/generating-api-document-in-nancy-using-swagger/
Looks like you still have to add swagger-ui separately.
Today I was moving a CakePHP app that I made on windows to my new macbook. For some weird reason one model doesn't load properly. Other models do load properly though, which confuses me...
I got this error:
Fatal error: Call to undefined method Locale::getLocale() in /server/cakephp/app/Controller/AppController.php on line 59
That line is just calling a method in my Locale model that I have.
So I tried to see what $this->Locale looked like with this code:
die(pr($this->Locale));
And this was the result:
Locale Object
(
)
I don't know why, but apparently I get an empty object. I tried removing the Locale.php file to see if CakePHP would automatically use AppModel, but it still becomes an empty object. So I tried searching if I have some empty class called Locale somewhere, but I couldn't find it.
Please help, this is so frustrating!
I cloned the CakePHP library from git yesterday, maybe that's useful information? Could it be that Locale has suddenly become a reserved word?
Permissions maybe? I've also had troubles in the past with hidden .files when moving cakephp apps, worth checking. I'm guessing that git handles both correctly though.
I'm writing a small application in CakePHP 2.1, and I want to use Nick Baker's file upload plugin. I downloaded the cakephp2.0 branch (I know that isn't done yet), and placed it in my apps Plugin folder. I made some necessary modifications on it, but I'm curious what the right way is to replace those App::import function calls (at start of FileUploadComponent, FileUploadBehavior and FileUploadHelper classes) with the App:uses function.
It needs to import the FileUploadSettings class from Config/file_upload_settings.php and the Uploader class from Vendor/upload.php. It can be done with the require_once function, but I'm sure there is a CakePHP way to do it.
From what I've gathered:
use import() for external libraries
and uses() for framework files
For example:
App::import('Vendor', 'ExternalLibrary');
App::uses('Inflector', 'Cake.Utility');
According to the Cake manual App::import() is comparable to the way require_once() works. From what I understand you would load classes using App:uses() and Vendor files using App:import().
The API documentation says the following on the subject:
All classes that were loaded in the past using App::import(‘Core’, $class) will need to be loaded using App::uses() referring to the correct package. This change has provided large performance gains to the framework.
The method no longer looks for classes recursively, it strictly uses
the values for the paths defined in App::build()
It will not be able to load App::import('Component', 'Component') use App::uses('Component', 'Controller');.
Using App::import('Lib', 'CoreClass'); to load core classes is no longer possible. Importing a non-existent file, supplying a wrong type or package name, or null values for $name and $file parameters will result in a false return value.
App::import('Core', 'CoreClass') is no longer supported, use App::uses() instead and let the class autoloading do the rest.
Loading Vendor files does not look recursively in the vendors folder, it will also not convert the file to underscored anymore as it did in the past.
The migration guide also has some things to say about App:uses() and is a good starting point in general to compare best practices for 2.0 with the older methods from 1.3 and lower.
This related question deals with loading Vendor files in Cake 2.0, I can't verify the claim by José Lorenzo that App:import() is a "silly wrapper" for require_once(), nor the statement that it's the preferred way of including files. The only reference I could find for the latter is in the Coding Standards for Cake contributors, viz. developers contributing to the Cake core, not applications built on the framework.
EDIT
Let's say you want to import the Twitter OAuth library, residing in Vendor/twitter, the main class file is twitteroauth.php in Vendor/twitter/twitteroauth/twitteroauth.php:
App::import('Vendor', 'twitteroauth', array('file' => 'twitter'.DS.'twitteroauth'.DS.'twitteroauth.php'));
I've seen the diagrams, but I want a description of how it all works -- for example -- cakephp uses the controller file and the view file. What happens? Is there anything out there? It would make using cakephp's mvc easier.
most simple request would look something like the following:
when you request a url, the router figures out what is needed and then uses the Dispatcher to open up the controller and run the corresponding method.
As the controller is fired up it includes and builds up the model that corresponds to that controller.
your method will then run and do what ever it needs to do.
When the controller is done calling all the code you have included the view class is executed which starts the rendering. It will include and render the corresponding view file and then the layout that has been set in the code.
all along the way there are a number of callbacks that are triggered in the various parts of the code, like controller::beforeFilter model::afterFind etc. Best to have a look a the api and book for more detailed information or ask a more specific question about that.
If you're at all familiar with Object Oriented code and php functions, you can start to read the CakePHP core methods. They will fill in a lot of blanks in terms of understanding the internal mechanics and relationships of Models Controllers and Views.
I'm running into a problem with GroupsController::build_acl()- http://book.cakephp.org/view/647/An-Automated-tool-for-creating-ACOs
It's taken me a while to track down the bug and now I've found it I'm not sure how to work around it.
Symptoms:
Not all methods for NodesController (defined by me) are returned.
Probable reason:
build_acl() imports a 3rd party plugin that also has a NodesController and a subsequent App::import() doesn't overwrite it.
I'm about to try two runs of the build, one with the plugin code commented out, but a more durable solution would be preferred!
I need a way to either drop an imported controller or force a re-import while remaining in scope.
you can not do what you want to do, think about straight php for a while. once you have used include('some/file.php'); how do you un-import it? you cant.
now the reason why you cant overwrite it is once again down to php. what happens if you run
<?php
include('some/file.php');
include('some/file.php');
?>
you will get errors about the class being defined already.
Cake is stopping this from happening, so the only (and correct way) is to not have 2 controllers with the same name. you can name them what ever you like and use the router to map to nice urls.
Turns out the plugin was redundant and not called anywhere in the application and would have broken it if it was as a class redefinition error would have ensued. After removal of the files everything worked okay.