I am new to cakephp. I need to send two variables to the view. In codeigniter its easy
$data['var1'] = 'One';
$data['var2'] = 'Two';
$this->load->view('myview',$data);
Now in Cakephp, I have a controller called function names search() in which I am sending an associative array to view.
$gal_providers = $this->GalProvider->getListByCategory(3,$location_id,false,9);
$this->set("gal_providers",$gal_providers);
But I need to send the variable $location_id too to the view. How can I send it ?
I read the article Using set() and compact() together, but I did not get the solution I was looking for.
The blog tutorial describes very well how to set data to the view. I recommend you to do the tutorial first, it gives you all you need to do your first steps.
You can set variables using $this->set() in your controller:
$this->set('first', 'second');
$this->set(array('foo' => 'bar', 'something' => 'else'));
The first will make the variable $first with the value second available in the view. The second will make $foo with value bar and $something with value else available in the view.
Controll::set() is setting the data to the view instances viewVars property. When the view is rendered it turns them into variables available in the view templates.
And do yourself and other people who look at your code a favour and follow the conventions and coding standards.
Related
I am working on a project with a PHP backend and Angular 1.x on the front end. I have a Listings model and I use a common template to create (add) and update (edit) a Listing.
Since eidt and add both use the same front end template I need to be able to detect when a user is creating and when they are editing. It seems there are several ways to do this:
I could pass a paramater in the ng-submit:
ng-submit="saveListing({{isNewListing}}"
Then I could read the value of the paramter in my controller, but I think this is overkill?
When editing a Listing some variables set for the form auto-fill
$scope.listing.id = x;
Therfore I could just check for a value in the above:
$scope.saveListing = function() {
if(listing.id) {
// update action
} else {
// save action
}
};
Is the second option a sound and non-hacky approach. I am not an Angular pro so although it seems the logical approach to me I want to ensure that I am not hot woring this.
I usually do something similar to the second approach. Since editing means you have to "get" the original record in most cases, the record should exist somewhere in the scope. I use ui-router and have a resolve for the record, which means I can check right at the top of the controller:
$scope.isEdit = record != null;
With a scope variable or similar (e.g. controllerAs vm) you can leverage the fact that you're in "edit mode" and change the UI up a bit. Instead of "+ New" on a button you can have "+ Save".
Hope that helps!
We have a large ERP system with angularJs as a front-end framework, and we are using the "check id" approach.
When updating/edit an item there would be existing id for that item.
I think the second approach is good and I don't see any drawbacks.
CODEIGNITER ISSUE: I can't seem to wrap my head around the array passed back from a model into a controller before sending it to the view.
My controller:
function getLatestComp()
{
$data['latest'] = $this->competitions_model->getLastCompetition();
echo json_encode($data['latest']);
$this->load->view('single_competition_view', $data);
}
How do I manipulate the returned array? Here is the returned array:
[{"comp_id":"25","song_id1":"178512356","song_id2":"119321744","start_date":"2015-03-23","end_date":"0000-00-00"},{"comp_id":"26","song_id1":"179391155","song_id2":"57429423","start_date":"2015-03-23","end_date":"0000-00-00"},{"comp_id":"27","song_id1":"57429423","song_id2":"52255807","start_date":"2015-03-23","end_date":"0000-00-00"},{"comp_id":"28","song_id1":"57429423","song_id2":"52255807","start_date":"2015-03-23","end_date":"0000-00-00"}]
say I just want the second instance's comp_id?
I have tried:
$data['latest'][1]->comp_id
$data['latest']->comp_id[1]
$latest[1]->comp_id
$latest[1]['comp_id']
How do I get the value I'm looking for?
It seems you want to access 2nd element of your object.
I think you returned data from your model like this get()->result()
if you want to get your 2nd element inside controller it should work
$data['latest'][1]->comp_id;//if your php version>=5.4
If your php version is lower then use this
$second_object=$data['latest'][1];
$second_object->comp_id;
similarly if you want get it inside your view use this way
$latest[1]->comp_id;//if php version>=5.4
//otherwise
$second_object=$latest[1];
$second_object->comp_id;
if you return data as array from your model like this ...get()->result_array() then you can accesses it easily
$data['latest'][1]['comp_id'];//inside your controller
$latest[1]['comp_id'];//inside your view
Hope this you understands it and solves your problem.
I would like to have different instances of the same view with different stores at the same time in an ExtJS application. At the moment i ceate multiple instances of the same view (Ext.view.View) in the viewport.
But what is the best practice to have a different store in every view? Every example that i found uses a Store-ID in the view that was created using the stores-Config of the controller. But this would use the same store for every view.
At the moment i figured the following possible solutions:
Create an own store class for every view instance. Add all stores to controller and use different Store-ID for every view instance.
Do not use stores of controller at all and create a new store in the initComponent of the view manually passing different parameters to each instance of the store.
Do not use stores of controller at all and create a new store in the initComponent of the view manually. Then use load to load the store manually using different parameters for each instance of the store.
Is any of this solutions the best practice or should it be done differently?
The thing with the stores array of the controller
is, that it will override any storeId defined. After loading the store class the controller set the storeId by a namespace convention, create the store and create the getter method method by using the value as the soreId.
Let me introduce option 4
Define one store for the view and require it in the view (you can also require it within the controller, just use the requires array).
Choose valid itemId's for the views and valid storeId's for your store that should depend on the itemId of the view (set it when creating the view!).
Within the initComponent create the storeId and lookup the store in the StoreManager. If it not exist create it and supply a Customer config and the storeId.
If you need to destroy the view and the store each time take a look at this post
Demo initComponent
initComponent: function() {
var me = this,
storeId = me.storeId + me.itemId;
me.store = Ext.StoreManager.lookup(storeId);
if(me.store === null)
me.store = Ext.create('App.data.CustomViewStore',Ext.apply({storeId: storeId},me.storeCfg || {}));
me.callParent(arguments);
}
Note: If you load the view with the views array you will end up with a getter that may not give you the view you expected. You may use the requires array of the controller here, too. If you want to use getter simply create your own one by using the refs config.
How can I get ID of current record, on Edit view layout?
You can load the Model in question and access it that way from within the view (which, in my opinion, is an awful idea) or you can 'set' it within your controller action:
// In the controller action that renders the view
$this->set('current_id',$this->ModelName->id);
// Access it this way in the view/layout:
<?php echo $current_id; ?>
You could also grab the current record ID using ajax, but that doesn't sound like something that would benefit you here.
Good luck
Use $this->data['ModelName']['id'] in the view
Since 2.3, as long as you have $this->Form->hidden('fieldname'); you can reliably use $this->Form->value('ModelName.fieldname') for the value of a field in the form. When the view is rendered, just as html input tags get a value based on the model used in the controller and referenced in the $this->Form-create('ModelName'), when rendering the $this->Form->value('ModelName.fieldname') has a value.
In the controller, $this->request->data['ModelName'] is set to an array of the Model's data in CakePhp, where ['id'] points to the value of the id field.
$this->Form->create('ModelName');
$this->Form->input('id');
$this->Form->end();
Corresponds to controller logic where $this->request->data['ModelName'] gets an associative array with at least "id" => some id value depending on the data read from the database.
Note that when you use $this->Form->value() to retrieve form data, it will work when the screen is initially rendered by the controller, but may fail when the screen is rendered again because a model validation rule that used a function in the model definition failed.
To make sure that the $this->Form->value() is reliable with more rigorous model validation logic, use $this->Form->hidden('fieldName'); to hold the data in $this->request->data['ModelName'] throughout multiple model validation iterations.
I'm new to CakePHP but I've been though their FAQs and guides to no avail. This is so simple that I just must not be thinking straight:
How can I access a parameter sent through the URL within my view files?
Example: http://example.com/view/6
How would I take that parameter ("6") and cycle it through the controller to another view page?
If that's too complex for a quick answer, how can I reference the 6 within the view page itself? The 6 in this situation is the "Id" value in my database, and I need to set it as the "parent" -
Thanks
Parameters can be retrieved like this
$this->params['pass']
Returns an array (numerically indexed) of URL parameters after the Action.
// URL: /posts/view/12/print/narrow
Array
(
[0] => 12
[1] => print
[2] => narrow
)
To access the parameter in your view look in $this->params
The URL, as you have it, will call the 6() method of your ViewController, which is not a valid method name. You may have to play with your routes to make that work.
If you don't want to configure your routes, you'll need the controller in the URL, like so:
http://example.com/thinger/view/6
which will call thingerControllerObject->view("6"). If you want "/view/" to go to a different method, edit the routes. See:
Cake controllers
Cake routes
Use the code below in the view file :
$url=Router::url($this->here, true);
$url_arr=explode("/",$url);
To see the content of $url been exploded simply print it using pr() as below :
pr($url_arr);
It will print associative array, thus you can access any particular number of parameter sent via url.