  
<div id="snippet_142" class="snippet">
  <h2><a href="/snippets/142-How-to-execute-a-CakePHP-controller-s-action-from-a-cron-job" title="How to execute a CakePHP controller's action from a cron job - PHP - cakephp, cron, job">How to execute a CakePHP controller's action from a cron job</a></h2>
  <div class="details">
    <a style="background-color: #FFFF94;" href="http://snippets.aktagon.com/languages/102-PHP">
      PHP</a> posted over 2 years ago by christian
          
  </div>

  <div class="body">
    <p>First copy webroot/index.php to <strong>webroot/cron_scheduler.php</strong>. Replace everything below <strong>require  CORE _PATH . &#8216;cake&#8217; . DS . &#8216;bootstrap.php&#8217;;</strong> with the following code:</p>


	<p><pre class="active4d"><span class="line-numbers">   1 </span> #
<span class="line-numbers">   2 </span> # BEGIN
<span class="line-numbers">   3 </span> #
<span class="line-numbers">   4 </span> # This was added to webroot/cron_scheduler.php
<span class="line-numbers">   5 </span> #
<span class="line-numbers">   6 </span> 
<span class="line-numbers">   7 </span> # Check that URI was specified and that we're called from the command line (not the web)
<span class="line-numbers">   8 </span> if($argc == 2 &amp;&amp; php_sapi_name() === &quot;cli&quot;) 
<span class="line-numbers">   9 </span> {
<span class="line-numbers">  10 </span> 	# Set request URI
<span class="line-numbers">  11 </span> 	$_SERVER['REQUEST_URI'] = $argv[1];
<span class="line-numbers">  12 </span> 	# Set user-agent, so we can do custom processing
<span class="line-numbers">  13 </span> 	$_SERVER['HTTP_USER_AGENT'] = 'cron';
<span class="line-numbers">  14 </span> 	
<span class="line-numbers">  15 </span> 	$Dispatcher= new Dispatcher();
<span class="line-numbers">  16 </span> 	$Dispatcher-&gt;dispatch($argv[1]);
<span class="line-numbers">  17 </span> } 
<span class="line-numbers">  18 </span> 
<span class="line-numbers">  19 </span> #
<span class="line-numbers">  20 </span> # END
<span class="line-numbers">  21 </span> #
<span class="line-numbers">  22 </span> # 
<span class="line-numbers">  23 </span> #
</pre></p>


	<p>Now you can execute CakePHP from the command line with the following command:</p>


	<p><pre class="active4d"><span class="line-numbers">   1 </span> $ php cron_scheduler.php /controller/action
</pre></p>


	<p>If you get the following error, remove the lines containing line feeds in bootstrap.php:</p>


	<p><pre class="active4d"><span class="line-numbers">   1 </span> Warning: Cannot modify header information - headers already sent by (output started at .../app/config/b
<span class="line-numbers">   2 </span> ootstrap.php:48) in .../app/app_controller.php on line 46
</pre></p>
  </div>

  <div style="font-size: 0.8em;margin:0.5em;">
    
      Tagged <a href="/tags/294-cakephp">cakephp</a>, <a href="/tags/353-cron">cron</a>, <a href="/tags/354-job">job</a>
    
    
  </div>
</div>



  
<div id="snippet_116" class="snippet">
  <h2><a href="/snippets/116-How-to-add-logging-to-CakePHP-applications" title="How to add logging to CakePHP applications - PHP - php, cakephp, logging, debug">How to add logging to CakePHP applications</a></h2>
  <div class="details">
    <a style="background-color: #FFFF94;" href="http://snippets.aktagon.com/languages/102-PHP">
      PHP</a> posted over 2 years ago by christian
          
  </div>

  <div class="body">
    <p>Note that Pear&#8217;s Logging package is a lot more flexible, so I recommend you use that instead of CakePHP&#8217;s built-in logging.</p>


	<p>Use this code to add a debug message to the CakePHP debug log:</p>


	<p><pre class="active4d"><span class="line-numbers">   1 </span> $this-&gt;log(&quot;Upload action accessed&quot;, LOG_DEBUG);
</pre></p>


	<p>Note that using something other than  LOG _DEBUG will log the message as an error.</p>


	<p>Run the code once and you should be able to see the message in $CAKE_APP/tmp/logs/debug.log.</p>
  </div>

  <div style="font-size: 0.8em;margin:0.5em;">
    
      Tagged <a href="/tags/279-php">php</a>, <a href="/tags/294-cakephp">cakephp</a>, <a href="/tags/295-logging">logging</a>, <a href="/tags/66-debug">debug</a>
    
    
  </div>
</div>






