Thursday, December 19, 2013

Generate XML using controller and force it to sent as download

Hi,

Goal: I have a button on a page. When user clicks it should generate a XML file and make it download (instead of opening in browser window).
Cake version: 1.3

So I create a HTML link like this:

<?php echo $html->link( __('Generate XML file', true), array('action'=>'generateXMLFile','ext'=>'xml'), 
array('class'=>'button','target'=>'_blank') ); ?>

Which outputs a simple HTML link. 

I have added in Routes:

      Router::parseExtensions('json','xml');

My action: 

function generateXMLFile(){
// Configure::write('debug',0);
// Only XML Requests
if(!$this->RequestHandler->isXml()){
die();

                   }
}


My view:

<response>
<posts>
<Post>
<id>test 234</id>
</Post>
</posts>
</response>

My Layout:

<?php header('Content-type: text/xml');?>
<?php echo $this->Xml->header(); ?>
<?php echo $content_for_layout; ?>


Now when I click the link the XML is sent as output to the screen. Clicking with right mousebutton ->save as.. makes it possible to save the file as XML. 
Headers are correctly set as text/xml

How can i prevent this from outputting to the screen but output it to a file/stream for download ?

thanks 


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

Re: the Form with method GET generate incorrent URL

Have You Found solution for this ??? I am also looking for this...

On Monday, November 1, 2010 8:53:36 AM UTC+5:30, DANNY wrote:
Hi All:
I look the  tutorial that Pagination for custom queries
(http://bakery.cakephp.org/articles/shird10/2010/08/29/pagination-for-
custom-queries
)

I can search the data by use URL (example.com/forums/viewtopic/x/y/
page:2
)
But I want to use Form(method is GET) to generate the url

<!-- ------search------ -->
<?php echo $this->Form-
>create('Order',array('action'=>'index','type'=>'get'));?>
<?php echo $this->Form->input('x');  ?>
<?php echo $this->Form->input('y');  ?>
<?php echo $this->Form->end(__('submit', true));?>
<!-- ------search------ -->

I press the summit button , I get the url is :
example.com/forums/viewtopic?x=AAAAAAA&y=BBBBBB

not example.com/forums/viewtopic/x/y/page:2
How to create the above Url by Form and method is GET

thanks

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

Re: Cakephp without Save() function?

You can create your insert statement and use the method Query(), but why don't you want to use the method Save?

Em quinta-feira, 12 de dezembro de 2013 02h18min56s UTC-4, YOONG SINJIE escreveu:
May I know , got other way to store the SQL statement, except the Save() function ? I want to do some simple calculation, i want to store the result at database. Anyone having any idea about it ?

public function add() { if ($this->request->is('post')) {
        /// complete calculation             $data1 = (string) $this->request->data['Calculate']['var1'];          $data2 = (string) $this->request->data['Calculate']['var2'];            echo $data1;          echo '<br/>';          echo $data2;            $result = $data1 + $data2;            echo '<br/>';          echo $result;          SQL Statement        mysql_query("INSERT INTO calculate VALUES('$data1','$data2','$result','2')") or die(mysql_error());         // if($this->Calculate->save($this->request->data)){           //     echo'succuful add';             // }      }
}


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

Re: CakePHP Newbie Questions

Hi Nick,

Well every thing has a learning curve so be patient and you'll get the gist of it. I've used almost all PHP frameworks and by far Cake is the one that is easiest to learn and use.

To answer your question: No, you don't need a "mysql" assigned to the controller. By "mysql" I guess you mean Model. Cake will try to load a view with the same name of the method in the controller. So if your Controller named "ProductsController" is called and within it the method "index", then Cake will load a view with name "index.ctp" from app/Views/Products. In the view you can put whatever you want.

Because cake is trying to give you a solid ground for a quick start if you have a model named "Product" then Cake will try to load it also. And if you don't have a DB table assigned to that model then Cake will be unhappy. If you don't want that just use property $uses and set it to array().

On Tuesday, December 17, 2013 7:21:50 PM UTC+2, Nick Harper wrote:
Hi,

I have recently started to learn CakePHP after just being used to coding from scratch.

I have installed a couple of plugins but the issue I have is linking them together, for example if I have a login script then how on earth do I just show the login page for example?

Does every Controller need to have a mysql assigned to it?

I have been through the blog tutorial but so far it just feels really restrictive compared to just coding from scratch but what really appeals is the plugins / helpers but I am just struggling with basic things like echoing the username they are logged in with when logged in etc.


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

Wednesday, December 18, 2013

Re: Looking for Help with Cakephp basics .. aka looking for a mentor

awesome answer reuben

+69

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

Re: Cakephp without Save() function?

When doing an insert, you need to call Model->create() first. And if you are calculating a value, assign is back to the request, so it can be saved on the Model.

$data1 = (string) $this->request->data['Calculate']['var1'];  $data2 = (string) $this->request->data['Calculate']['var2'];    $result = $data1 + $data2;
$this->request->data['Calculate']['result'] = $result;
$this->request->data['Calculate']['otherField'] = 2;
$this->Calculate->create();
if ($this->Calculate->save($this->request->data)){
   echo 'Successful add';
}

On Thursday, 12 December 2013 16:18:56 UTC+10, YOONG SINJIE wrote:
May I know , got other way to store the SQL statement, except the Save() function ? I want to do some simple calculation, i want to store the result at database. Anyone having any idea about it ?

public function add() { if ($this->request->is('post')) {
        /// complete calculation             $data1 = (string) $this->request->data['Calculate']['var1'];          $data2 = (string) $this->request->data['Calculate']['var2'];            echo $data1;          echo '<br/>';          echo $data2;            $result = $data1 + $data2;            echo '<br/>';          echo $result;          SQL Statement        mysql_query("INSERT INTO calculate VALUES('$data1','$data2','$result','2')") or die(mysql_error());         // if($this->Calculate->save($this->request->data)){           //     echo'succuful add';             // }      }
}


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

Re: Yet Another Validation Question

Have you confirmed that the validation function is actually getting called, via CakeLog::debug() or error_log(print_r($data, true));

If your validation function only returns false, and it never gets called, mostly likely, validation is not getting called at all.

If the data is data['Community']['start']['year'], then you're probably using the FormHelper date functions, which tend to split a date into it's components, and return the results as an array.  However, you may wish to confirm with  error_log(print_r($data, true)); or similar, the structure of the field at validation time.  You may want to check for $value $this->data[$this->alias][$field]['year']; when determining the value of the year.

Regards
Reuben Helms

On Wednesday, 18 December 2013 16:22:45 UTC+10, advantage+ wrote:

I cannot validate my year select.

 

When posted it appears as :

data[Comunity][start][year]...

2009

 

Validation rule:

'start' => array(

       'required' => true,

       'allowEmpty' => false,

       'rule' => array('checkYearValues', 'start'),

       'message' => 'Please enter a year between 1950 and 2013 when you started.',

       'last' => true),

 

checkYearVales:

       /************************************************************

       * FORM VALIDATION SELECT YEAR VALUES

       * ***********************************************************

       * @access public

       * @return bool

       *

       *

       *

       ************************************************************/

       public function checkYearValues($data, $field){

 

                     $value = $this->data[$this->alias][$field];

                    

                     $baseYear = date('Y') - 2013 + 10;

                     $years =  array_combine(range( date('Y'), date('Y') - $baseYear), range( date('Y'), date('Y') - $baseYear));

                    

                     //return in_array($value, $years);

                     return false

                    

       }

 

But it always passes validation

 

Any ideas why start[year] gets added to the input?

 

Thanks all

 

Dave Maharaj

Freelance Designer | Developer
Description: header_logo

www.movepixels.com  |  dave@movepixels.com  |  709.800.0852

 

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.