In my application I did not have model.am using webservice for data.To handle the exception I need use $this->invalidate from controller. Without model how to use that?
Thanks
invalidate() is a method from AppModel, so it must be called against a model. If you're doing validation, why aren't you using a model?
For using a webservice other patterns work much better. For example you could build a custom datasource:
http://book.cakephp.org/2.0/en/models/datasources.html
That's the best solution in most cases. Now you put everything in a controller and that will cause issues.
So the advised structure for the data flow is:
datasource -> model (with optionally a behaviour) -> controller
If it's a common webservice I would suggest that you put the datasource in a plugin to seperate it out. Like: https://github.com/cakephp/datasources as an example (be aware they're a but old). See for example this one as a more recent: https://github.com/dkullmann/CakePHP-Elastic-Search-DataSource
In general you tried to put all in the controller to keep it simple I suspect but it is advised to structure it according to the framework.
Related
We're looking at moving our Integration Tests for our API to CakePHP's version of PHPUnit. We need to test that the returned values from the API are sane (valid JSON, etc...)
The helper methods on the IntegrationTestCase abstract class look really useful, allowing me to simulate requests by simply calling $this->get('/articles') or similar, but from what I can tell there's no way to actually read the response to one of these requests. Am I missing something?
It seems that without the helper methods provided by IntegrationTestCase it would be much harder to make requests. So what's my best option here?
I just realised that I can do this by simply accessing $this->_response. At first I didn't think I could do this because its visibility is protected, but then I realised that I can access it because my test inherits from IntegrationTestCase.
I'm currently learning ExtJs, but I can't seem to grasp my mind arround the following.
What is the difference between the array notation and the requires notation
For example:
view['MyPanel']
model['MyModel']
controller['MyController']
store['MyStore']
requires: ['namespace.view.MyPanel']
Do they do the same or ... ?
And why do I have to put ALL the views, models, controllers and stores used in the application inmediatley in the app.js?
Can someone clear those thing out to me? :)
Requires will just load the files matching the class name from the server. It will not instantiate anything. You should require what is needed in each view/controller/model, you don't need to have it all in app.js.
For instance, if you have a MyModel that has a relation to MySubModel, then MyModel would require MySubModel. Requires is essentially about loading other classes when needed so that they are fetched from the server before being used - since using a class when it is not loaded creates a noticable delay due to ExtJS having to fetch the class from the server before instantiating it.
The models, views and controllers arrays in a controller are a convinient way to require such resources as you don't have to specify the path to the models/controllers/views folders. See the docs on controller models config for example.
A guide on how to structure your application is available here even though I don't really like that they load all views and all controllers in this approach. But it's a good start. You can dynamically load stuff later on when your application grows.
After upgrading to 2.0 many "hacks" like accessing protected attributes are not possible anymore.
For example (AssetHelper):
$scripts = $this->View->_scripts;
//pack and return combined scripts
The helper fatal errors, of course.
Does anyone know how to access those attributes in 2.0 without creating too much overhead in the process?
Are the AssetHelper and other such classes outdated if they try to access the View from inside a Helper?
The new way of accessing the View from a Helper in 2.0 is $this->_View which is protected. You can see it on the Helper API page.
I looked at the AssetHelper on Github and it is outdated. It still retrieves the view from the ClassRegistry instead of the new method. It also accesses the __scripts attribute of the old 1.3 View class which wasn't actually "private". I think you're correct that the change to real visibility declarations has broken this use.
Just brainstorming, but I wonder if you could make your own View class that has a getter for the _scripts attribute like $this->_View->getScripts(). I know that in 2.0 they added a slick ability to alias core classes; while I think this is restricted to helpers, components and behaviors it's something to think about.
Hope that helps.
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 developing a CakePHP application that we will provide as a white label for people to implement for their own companies, and they'll need to have certain customization capabilities for themselves.
For starters, they'll be able to do anything they want with the views, and they can add their own Controllers/Models if they need to add completely new stuff. However, I'd rather advise against touching my controllers and models, to make version upgrading easier.
Esentially, the customization capabilities I'm planning to give them are going to be quite basic, I just need to call "something" when certain things happen, so they can do things like update external systems, e-mail themselves/the clients, things like that.
I'm wondering what's the best way to do this?
My plan is to have a "file" (with one class) for each controller of mine, to keep things reasonably organized. This file will have a bunch of empty methods that my code will call, and they'll be able to add code inside those methods to do whatever they need to do.
The specific question is, should this class full of empty methods be a Component? A Controller? Just a regular plain PHP class?
I'll need to call methods in this class from my Controllers, so I'm guessing making it a Controller is out of the question (unless maybe it's a controller that inherits from mine? or mine inherits from theirs, probably).
Also, I'd need the implementer of these methods to have access to my Models and Components, although I'm ok with making them use App::Import, I don't need to have the magic $this->ModelName members set.
Also, does this file I create (etiher Component or Controller) have to live in the app folder next to the other (my) controllers/components? Or can I throw it somewhere separate like the vendors folder?
Have you done something like this before?
Any tips/advice/pitfalls to avoid will be more than welcome.
I know this is kind of subjective, I'm looking to hear from your experience mostly, if you've done this before.
Thanks!
Two ideas that spring to mind:
create abstract templates (controllers, models, whatever necessary) that your clients can extend
write your controllers/components/models as a plugin within their own namespace
Ultimately you seem to want to provide an "enhanced" Cake framework, that your clients still have to write their own Cake code in (I don't know how this goes together with your idea of "basic customization capabilities" though). As such, you should write your code in as much an "optional" manner as possible (namespaced plugins, components, AppModel enhancements, extra libs) and provide documentation on how to use these to help your clients speed up their work.
I would setup a common set of events and use something like the linked to event system to handle this.
That lets the clients manage the event handler classes (read the readme to see what I mean) and subscribe to and broadcast events application-wide.
Also - if you want to have your users not muck about with your core functionality I recommend you package your main app as a plugin.
http://github.com/m3nt0r/eventful-cakephp