I am trying to display an uploaded image with the Media Plugin of CakePHP.
I added the helper to the controller helper array: var $helpers = array('Media.Media');. Then, in my view, I have this code: echo $media−>file($news['Attachment'][0]['dirname'].DS.$news['Attachment'][0]['basename']);. But the problem is that, it outputs this error:
Undefined variable: media− [APP/views/news/view.ctp, line 3]
What could be the problem?
By the way, if a plugin has a model User in app/plugin/users/models/user.php and i create a new model called User in the app/models folder which one will be loaded?
Thanks in advance for any help!
First off if you are using 1.3.x refer to helpers via $this->HelperName->method(), there could be a variable called $media being set in some method. you can check this by doing var_dump($media);
The other option is that something has maybe unset it. Its very strange that you have the helper set but the variable is not set. It could also be due to adding the $helpers array to the wrong controller, you can try add it to app_controller and see if that works. if it does you had it in the wrong place.
If i got your second question correct, and we are talking about auto loading, a plugin controller will first look for the model in its own plugin directory, if it is not found there it will fall back to the app/models directory.
if you are loading it manually via the $uses array, it depends on the version of cake and how you do it. In previous versions 1.x even $uses = array('User'); would load the plugin model as cake would auto add the plugin prefix. This has changed for 2.0 afaik.
For other methods of loading a model, such as $this->loadModel('User); would load from app/models and $this->loadModel('PluginName.User') would load from the app/plugins/plugin_name/models dir.
Edit:
you are right that is funny having the error show $media- and there is the problem. did you copy that code from some site? − is not - you have a utf8 char in the code which is what its complaining about.
Related
My understanding of the CakePHP2 doc leads me to believe I can create a simple plugin that consists of a single view and that view can (if done correctly) override the default view.
For example, if the application has a app/View/Pages/foo.ctp that simply displays the text "Foo", then a Plugin in app/Plugin/Bar/View/Pages/foo.ctp that contains the text "Bar"
I've made sure that the bootstrap.php loads the plugin with CakePlugin::load('Bar');
And I've deleted app/tmp/cache//
From what I understand I'm not required by Cake to need a Controller or Model.
Yet, the application only displays "Foo" instead of the intended override of displaying "Bar".
From what I've described, what parts of my understanding or implementation approach seem wrong? (And why?) And what are the simple/better ways to implement this plugin view?
TIA
(I have read http://book.cakephp.org/2.0/en/plugins.html and http://book.cakephp.org/2.0/en/views.html)
Themes?
A theme is just a collection of template files that can be used to override the templates used in an application; whereas plugins also contain classes - are you looking for themes?
Creating a template in a plugin is not enough
Alone is not sufficient to just create a template file and load a plugin. To render it you'll need to either:
Explicitly render the file
I.e. In any controller:
$this->render('Bar.template_name')
Create an actual plugin
I.e. Create plugin files:
$ Console\cake bake plugin Bar
$ Console\cake bake controller Some --plugin Bar
$ etc.
$ echo "Something" > Plugin/Bar/View/Some/index.ctp
And request: http://yourapp.com/bar/index
See the docs on how to create a plugin for more information.
I'm new in CakePhp but experienced in CodeIgniter. I created a controller in "WelcomeController.php" in controller directory and run the page. I got two errors
1. Error: The view for WelcomeController::index() was not found.
2. Error: Confirm you have created the file: C:\xampp\htdocs\myc\app\View\Welcome\index.ctp.
My question
Why I am getting this error even though I have supplied index() function?
In codeigniter we may not create a directory for a view. I don't want to create a directory "Welcome" in view. I there any provision provided?
In Cakephp you have to create view for function or here it called action. In your case, Create index.ctp on App->View->Welcome folder. This Getting Start
will give you a basic idea.
1) You're getting that error because your missing the view, not the controller function. To fix, do what the error suggested:
Confirm you have created the file: C:\xampp\htdocs\myc\app\View\Welcome\index.ctp.
2) "I don't want to create a directory "Welcome" in view. I there any provision provided?".
Not really... I mean, no if you want that action to have a correspondent view to put the content. Otherwise you can use $this->autoRender = false to not show anything... But that'll mean the url localhost/welcomes/index will be blank.
I recommend you read the basics as Fazal said. I know every framework can give us "quirks" and we end up expecting every other framework to work the same way we are used to, but try to adapt to the cake-way.
Btw, should be "WelcomesController", according to cake conventions
Be sure that you have all the files and directories necessary for the modal, controller and view to link up correctly i.e.: Create the folder called welcome(s) in your views directory with an index.ctp file. This should get rid of that error.
Check out this brilliant blog tutorial: Link
I have an ItemsController which extends AppController.
To handle my site menu, I used a menuhelper.
However, I saw in the documentation that you have to add your helpers in an array in your controller (AppController):
public $helpers = array('Form', 'Html', 'Menu');
The weird thing is that I forgot it and my menu worked anyway.
Also, you always have to add Form and Html extra to the array because otherwise they don't work anymore.
However, when I do something like this in my AppController, my form helpers still work:
public $helpers = array('Menu');
So it seems that whatever I do, it still works, but I don't get why and I don't like automatic "magic" :)
Is there something I am missing in the docs?
Since 2.x you don't "need" to specify app or core helpers.
Those are lazyloaded automatically.
You only need to specify plugin helpers manually.
That said I personally still always describe what helpers I use, just to be consistent with the plugin ones.
I have a method which exports riport data into an xlsx file in my CakePHP 2 project. It is working well. In the same time it seems it just ignores anything what I put into View/Layouts/xlsx/default.ctp
In my routes.php I have
Router::parseExtensions('json', 'xlsx');
In my controller I have
public $components = array('RequestHandler');
My View/Riports/xlsx/export.ctp is rendered, but View/Layouts/xlsx/default.ctp is ignored.
What do I miss?
I think you're out of luck here. From the documentation;
The data view classes don’t support layouts. They assume that the view file will output the serialized content.
http://book.cakephp.org/2.0/en/views/json-and-xml-views.html#using-a-data-view-with-view-files
But I may be wrong of course :)
I am trying to learn about David Persson's CakePHP Media Plugin through the included tutorial.
I get to the point where I can debug $this->data and it shows the 'file' array with the plugin's added keys (name, size and type) and I've added var $actsAs = array('Media.Transfer'); to the model of my choice. The problem is, at this point when I submit the form, it should be uploading the file to the app/webroot/media/transfers, but it is not doing that.
Could the problem be that I haven't added any controller code or is that taken care of by asserting that var $actsAs = array('Media.Transfer');? Any other ideas? Do you need to see any code to diagnose the problem?
You need to also use the Media.Coupler behavior.
var $actsAs = array('Media.Transfer', 'Media.Coupler');