sonam
sonam

Reputation: 3760

run symfony 2 command from a php file

I wanted to add a symfony command as cron job but my hosting server (2freehosting.com) does not allow me to add following command:

 php app/console cache:clear --env=prod --no-debug

Only php path/to/file type of command is allowed.

So I want to create a file clearCache.php to run above command and add it as cron job

php path/to/clearCache.php

How can I call this symfony command in clearCache.php ?

My directory structure:

-app
-bin
-vendor
-src
-public_html
  -bundles
  -app.php
  .....
-clearCache.php

Upvotes: 1

Views: 646

Answers (3)

Altynbek Usenov
Altynbek Usenov

Reputation: 216

Remember that clear cache means remove folders and files in app/cache. So you can use this trick

Create clear.php file in web folder and put

<?php // get all file names
$str = __DIR__.'/../app/cache/';

function recursiveDelete($str){
if(is_file($str)){
    return @unlink($str);
}
elseif(is_dir($str)){
    $scan = glob(rtrim($str,'/').'/*');
    foreach($scan as $index=>$path){
        recursiveDelete($path);
    }
    return @rmdir($str);
}
}

recursiveDelete($str);

//here you can redirect to any page
echo(__DIR__.'/../app/cache/*'.' -> Clear completed'); 

Then access by localhost/clear.php or yoursite.com/clear.php

Upvotes: 0

mattexx
mattexx

Reputation: 6606

assuming you are using linux, you could create an alias:

alias symfony-cc="php path/to/app/console cache:clear --env=prod --no-debug"

Upvotes: 0

l3l0
l3l0

Reputation: 3393

You can use bash script like clearCache.sh

#!/bin/bash

/path/to/php /path/to/app/console cache:clear --env=prod --no-debug

Upvotes: 1

Related Questions