2017-05-29 40 views
1

我的控制器的内核是这样代码删除表中的记录,但犯规加

protected function schedule(Schedule $schedule) 
{ 
    $schedule->call('\App\Http\Controllers\[email protected]')->everyMinute(); 
} 

当我打电话控制器

namespace App\Http\Controllers; 
use Illuminate\Http\Request; 
use DB; 
use Illuminate\Support\Facades\Storage; 
use App\news; 
use Auth; 
use DOMDocument; 
use Exception; 

class HomeController extends Controller 
{ 
    public function automatic() 
    { 
     function delete(){ 
      DB::table('news')->delete(); 
      echo "table deletted"; 
     } 
     delete(); 
    }  
} 

它删除表中的记录。但是当我打电话给这个代码的控制器

class HomeController extends Controller 
{ 
public function automatic() 
{ 
function follow_links_reportersnepal($url,$cat) 
    { 
     ini_set('max_execution_time', 9000); 
     global $already_crawled; 
     global $crawling; 
     $i=0; 
     $doc = new DOMDocument(); 
     @$doc->loadHTML(@file_get_contents($url)); 
     $linklist = $doc->getElementsByTagName("a"); 
     $already_crawled[]="sailaab"; 
     foreach ($linklist as $link) 
     { 
      try { 
       $l = $link->getAttribute("href"); 
       if(strlen($l)==45) 
       { 
        if (!in_array($l, $already_crawled)) 
        { 
        $i++; 
        if ($i>2) { 
        break; 
        } 
        $already_crawled[] = $l; 
        $content = file_get_contents($l);  
        $first_step = explode('<h3 class="display-4">' , $content); 
        $second_step = explode('</h3>' , $first_step[1]);//title 

        $third_step=explode('<div class="entry-content">',$second_step[1]); 
        $fourth_step=explode('<p>',$third_step[1]); 
        $fifth_step=explode('<div class="at-below-post', $fourth_step[1]); 

        $sixth_step=explode('<figure class="figure">', $content); 

        if(isset($sixth_step[1])){ 
         $seventh_step=explode('src="', $sixth_step[1]); 
         $eighth_step=explode('"', $seventh_step[1]); 
         $url = $eighth_step[0]; 
         $img=rand();  
         $img=(string)$img; 
         file_put_contents($img, file_get_contents($url)); 

         $user = Auth::user(); 
         news::create([ 
         'news_title'=>strip_tags($second_step[0]), 
         'category_id'=>$cat, 
         'source_id'=>'reportersnepal', 
         'reference_url'=>"www.reportersnepal.com", 
         'reference_detail'=>$l, 
         'news_summary'=>"null", 
         'news_detail'=>strip_tags($fifth_step[0]), 
         'news_image'=>$img, 
         'news_video'=>"null", 
         'news_status'=>"1", 
         'created_by'=>$user->id, 
         'last_updated_by'=>$user->id, 
         ]); 
        } 
        else{ 
         $user = Auth::user(); 
         news::create([ 
         'news_title'=>strip_tags($second_step[0]), 
         'category_id'=>$cat, 
         'source_id'=>'reportersnepal', 
         'reference_url'=>"www.reportersnepal.com", 
         'reference_detail'=>$l, 
         'news_summary'=>"null", 
         'news_detail'=>strip_tags($fifth_step[0]), 
         'news_image'=>"default.png", 
         'news_video'=>"null", 
         'news_status'=>"1", 
         'created_by'=>$user->id, 
         'last_updated_by'=>$user->id, 
         ]); 
        } 
        }     
       } 

      } catch (Exception $e) { 
       continue; 

      }   
     } 
    } 

    follow_links_reportersnepal('http://reportersnepal.com/category/featured','1'); 
} 
} 

它没有写任何东西在我的数据库表中。当我回显变量时,它显示数据。 当我手动调用它们时,此代码正常工作。

而我的cron选项卡

php-cli -q /home/allnewsnepal/public_html/artisan schedule:run 
+0

是从命令行调用的吗? – sumit

+0

以及你如何安排它?你可以发帖 – Exprator

+0

是的,我已经给出了细节。 在命令行中我称之为 php artisan日程安排:运行 –

回答

0

Laravel调度是基于命令行的,你不能使用会话和验证的组件有

低于你的代码不要在这里做任何意义

$user = Auth::user(); 

您需要将用户信息存储在内存数据库中的某个其他数据库中,如redis,然后使用它

+0

我已经添加了我的代码的一些细节。 –

+0

你不能从命令行访问会话,它是纯粹基于浏览器的。 – sumit

+0

但我认为调度程序只调用控制器,而控制器正在使用身份验证。但是,当我使用删除功能它删除。当我在控制器(delete和follow_links_reporters_nepal)中都具有这两个函数时,delete函数将被删除。但是follow_links_nepal会回显变量中的数据,但不会写入数据库。 –