从php文件中运行symfony 2命令

时间:2013-07-27 15:21:04

标签: symfony symfony-2.3

我想添加一个symfony命令作为cron作业但我的托管服务器(2freehosting.com)不允许我添加以下命令:

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

仅允许php path/to/file类型的命令。

所以我想创建一个文件clearCache.php来运行上面的命令并将其添加为cron job

php path/to/clearCache.php

如何在clearCache.php中调用此symfony命令?

我的目录结构:

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

3 个答案:

答案 0 :(得分:1)

您可以使用像clearCache.sh

这样的bash脚本
#!/bin/bash

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

答案 1 :(得分:0)

假设您使用的是linux,您可以创建一个别名:

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

答案 2 :(得分:0)

请记住,清除缓存意味着删除app / cache中的文件夹和文件。所以你可以使用这个技巧

在web文件夹中创建clear.php文件并放入

<?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'); 

然后通过localhost / clear.php或yoursite.com/clear.php访问