CakePHP: Bad format URLs - php

I'm trying to create a simple form in CakePHP detached from a model, I'm having problems with the generated URL:
I get this: (doesn't work -> paypal lowercase)
http://local.dev/integration-cloud/public_html/paypal/checkout
Instead of this: (works -> Paypal camel case)
http://local.dev/integration-cloud/public_html/Paypal/checkout
This is my code:
Paypal/view.ctp
<?php echo $this->Form->create(false, array('action' => 'checkout')); ?>
<?php echo $this->Form->end('Finish'); ?>
UPDATE:
If I try this:
<?php echo $this->Form->create(false, array('url' => array('controller' => 'Paypal', 'action' => 'checkout'))); ?>
<?php echo $this->Form->end('Finish'); ?>
It's work, but I don't want to write on all my views the reference's controller, is there another way to do this?
btw I'm on a Linux server, could be a case sensitive problem?
Thanks,

cakephp conventions want controller name in url to be lowercase and underscored
http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html#url-considerations-for-controller-names
It works even with uppercase controller names but you have to set it manually in every form

You can do as below if you want:
<?php echo $this->Form->create(false, array('url' => '/Paypal/checkout')); ?>
<?php echo $this->Form->end('Finish'); ?>
But the below one is recommended in CakePHP:
<?php echo $this->Form->create(false, array('url' => array('controller' => 'Paypal', 'action' => 'checkout'))); ?>
<?php echo $this->Form->end('Finish'); ?>

The problem here was a mix up between the Paypal's plugin and the name of my controller PaypalController.
public $name = 'Paypal';
When I call paypal (lowercase) CakePHP is trying to call the plugin instead my PaypalController class.
http://local.dev/integration-cloud/public_html/paypal/checkout
So to fix this problem I had to change the name of my controller to PaypalPaymentProcessor

Related

Function that creates a input name from a string in CakePHP

I would like know if there is a function in CakePHP that transforms Mymodel.Mycolumn into data[Mymodel][Mycolumn].
I know how to do this with PHP only, but I would like to know if there is a built-in CakePHP function for it.
EDIT:
I don't need the input, only the name.
See the pretty complete CakePHP Cookbook. To use the current model:
echo $this->Form->input('Mycolumn');
Or to specify the model:
echo $this->Form->input('Mymodel.Mycolumn');
Creates:
<input type="text" id="MymodelMycolumn" name="data[Mymodel][Mycolumn]">
if you're sending, you can do this:
<?php
echo $this->Form->create('Mymodel');
echo $this->Form->input('Mymodel.Mycolumn', array('label' => 'add data for Mycolumn'));
echo $this->Form->submit('submit', array('class' => 'form-submit', 'title' => 'Enter'));
?>

cakephp pagination change urls to make them more seo friendly

I am using this syntax:
<ul>
<?php echo $this->Paginator->prev($this->Html->image('arrow_left.png', array('border' => 0, 'tag'=>'li')), array('escape'=>false, 'tag'=>'li')); ?>
<?php echo $this->Paginator->numbers(array('separator' => '', 'tag'=>'li')); ?>
<?php echo $this->Paginator->next($this->Html->image('arrow_right.png', array('border' => 0, 'tag'=>'li')), array('escape'=>false, 'tag'=>'li')); ?>
</ul>
which creates me links like
<li>
4
Is there any way, that I can get rid of the controller and action in the url and use instead the url parameter?
I mean instead of getting links like
http://domain.com/mycontrollername/search/cat:1/page:2?url=slugged_url
I would like to get something like
http://domain.com/slugged_url/cat:1/page:2
Is that possible?
If yes, how?
I have the same issue with results per page:
<?php
$sortDir = ($this->Paginator->sortDir() == 'asc') ? $this->Paginator->sortDir(): 'desc';
$text = ($sortDir == 'asc')? __('aufsteigend', true):__('absteigend', true);
echo $this->Paginator->sort('OBJ_PREIS', $text, $options = array('escape' => false, 'direction'=>'DESC', 'class' => 'asc'))`;
?>
Can I change the urls there as well?
Please advice!
Thanks!!
This is possible using routes. A route will rewrite the URL to something "pretty".
Check the offical CakePHP book. It has a complete chapter just about routing.
The book as plenty of examples for different things you can do with routes as well.

Basic Hidden field in yii

I'm trying to place data in hidden text in yii, but I don't know how.
I need a similar code to a regular php syntax:
<input type="hidden" name="field_name" value="a"/>
It's supposed to be a field with static value of a. I just need it to go with my $_POST variables for error checking.
Is it possible to avoid modifying the models and controllers just to put the field in?I cant use gii cause I only have snippets of code with me.Sorry as well as I have little understanding of yii so I have no clue if what I'm saying about the last 2 sentences is correct.
in views
hidden field with model and form:
<?php echo $form->hiddenField($model, 'name'); ?>
or without model
<?php echo CHtml::hiddenField('name' , 'value', array('id' => 'hiddenInput')); ?>
Yii hidden input :
<?php echo $form->hiddenField($model,'fieldName',array('value'=>'foo bar')); ?>
In Yii2 this has changed too:
<?= Html::activeHiddenInput($model, 'name') ;?>
References:
http://www.yiiframework.com/forum/index.php/topic/49225-activeform-how-do-you-call-label-input-and-errors-individually/
https://github.com/yiisoft/yii2/issues/735
if data from database and value or size field:
echo $form->hiddenField($experience,'job_title',array('size'=>'50','value'=>$experience_data['job_title'])); ?>
Yii 1
<?php echo $form->hiddenField($model, 'name'); ?>
Yii2
<?= Html::activeHiddenInput($model, 'attribute', ['value' => 'Some Value']) ?>
Also, worth noting for Yii2, the array parameter works different to a normal form field.
E.G. A normal input would look more like this.
<?= $form->field($model, 'attribute', ['inputOptions' => ['placeholder' => 'Some Placeholder', 'value' => 'Some Input Value']]) ?>
Hope this helps.
for yii2 you can try this
<?= $form->field($model, 'user_type',['inputOptions' => ['value' => '2']])->hiddenInput()->label(false) ?>
It worked for me
Alternatively,
echo CHtml::activeHiddenField($model,"[$i]id", array("value" => $model->id));
This would set hidden field value as the id from model. The [$i] is useful for multiple record update.
Here are two ways to do that...
without model
echo CHtml::hiddenField('name' , 'value', array('id' => 'name'));
with model
echo $form->hiddenField($model, 'name');

Ajax request won't trigger. [Yii framework]

I have a little problem with a Controller method AJAX call in Yii. The thing is that I'm trying to filter the data of one dropDownList based in the value of a previous selected item.
In the view file, where I figured out is the source of the problem, I have this piece of code:
<?php echo $form->labelEx($model,'Estado'); ?>
<?php echo $form->dropDownList($model,'estado',CHtml::listData(Estado::model()->findAll(),'id','nombre'),array(
'ajax'=>array(
'type'=>'POST',
'url'=>CController::createAbsoluteUrl('buscar/select'),
'update'=>'#'.CHtml::activeId($model,'tbl_municipio_id'),
),
'class'=>'form-control'
));
?>
<?php echo $form->error($model,'Estado'); ?>
On the Controller side, I got this:
public function actionSelect(){
echo "Hello world";
$data = Municipio::model()->findAll('tbl_estado_id=:tbl_estado_id',
array(':tbl_estado_id'=>(int) $_POST['Consultorio_estado']));
$data = CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
The ajax call to the Select method isn't triggered when the dropDownList is clicked. I tracked the request using Firebug and no error nor fail message is dropped.
Anyone knows what can I do?.
Thanks in advance.
With my knowledge in Yii 1.1.13, there is no such option for ajax for form->dropDownList, just Chtml::dropDownList does.
Therefore you have option to manually custom event change of form->dropDownList or add more jQuery script to handle it by yourself, or simply switch to use Chtml::dropDownList like below example
<?php
echo CHtml::dropDownList('inst_province','',
array(1=>'A',2=>'B',3=>'C', 4=>'D'),
array(
'prompt'=>'Select City',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('city/selectAll'),
'update'=>'#city_area',
'data'=>array('city_param'=>'js:this.value'),
)));
?>
http://www.yiiframework.com/wiki/429/an-easy-solution-for-dependent-dropdownlist-using-ajax/

CakePHP is it possible to instance multiple controller actions in one view

I'm a little bit of a beginner to CakePHP and PHP in general, but I have OOP experience.
I'm trying to make a mini Twitter to get used to the Cake framework.
I have a PostsController class that handles all creating blog posts, editing deleting etc, but I'm having trouble adding and add post form to the same page above the View Posts.
i.e. adding posts works fine when I link to a new page
<p><?php echo $this->Html->link('Add Post', array('action' => 'add')); ?></p>
but while trying to put a form in the same page as the view I don't know how to call the 'add' action to save and use the data taken in in the form.
echo $this->Form->create('Post',array('action' => 'add'));
echo $this->Form->input('title');
echo $this->Form->input('body', array('rows' => '3'));
echo $this->Form->end('Save Post');
echo $this->Form->create('Post', array('action' => 'add'));
echo $this->Form->input('title');
echo $this->Form->input('body', array('rows' => '3'));
echo $this->Form->end('Save Post');
putting random arrays in your code will not do anything.
Missing semicolon after the array
Not assigning or using the array in anything.
Trying putting your array as the second parameter in the form create method.

Categories