Php 在schedular中调用的代码删除表中的记录,但不添加
我的控制器内核是这样的Php 在schedular中调用的代码删除表中的记录,但不添加,php,laravel-5,cron,scheduler,Php,Laravel 5,Cron,Scheduler,我的控制器内核是这样的 protected function schedule(Schedule $schedule) { $schedule->call('\App\Http\Controllers\HomeController@automatic')->everyMinute(); } 当我呼叫控制器时 namespace App\Http\Controllers; use Illuminate\Http\Request; use DB; use Illuminate
protected function schedule(Schedule $schedule)
{
$schedule->call('\App\Http\Controllers\HomeController@automatic')->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');
}
}
Laravel scheduler基于命令行,您不能在那里使用会话和身份验证组件 下面的代码在这里没有任何意义
$user = Auth::user();
您需要将用户信息存储在一些其他内存中数据库上,如
redis
,然后使用它Laravel scheduler基于命令行,您不能在那里使用会话和身份验证组件
下面的代码在这里没有任何意义
$user = Auth::user();
您需要将用户信息存储在另一个内存数据库中,如
redis
,然后使用它它是从命令行调用的吗?您如何安排它?你能贴出来吗?是的,我已经给出了细节。在命令行lline中,我称之为php artisan schedule:RunIt是从命令行调用的吗?您是如何调度它的?你能贴出来吗?是的,我已经给出了细节。在命令行lline中,我称之为php artisan schedule:runYou无法从命令行访问会话,它完全基于浏览器。但我认为调度器只调用控制器,而控制器使用的是Auth。但当我使用delete函数时,它会删除。当我在控制器中有两个函数(删除和跟踪链接)时,删除函数将删除。但以下链接将在变量中回显数据,但不会在数据库中写入。您的控制器将使用auth组件,当且仅当您直接从控制器调用它时,尝试为$user\u id添加一些静态值,它将工作,并且您将了解您的问题它仍然不工作。我把静态值放在$user_idHey thanx中,它起作用了。是Auth造成了这个问题。无论如何,谢谢你的帮助。:)您不能从命令行访问会话,它完全基于浏览器。但我认为调度程序只调用控制器,而控制器使用的是Auth。但当我使用delete函数时,它会删除。当我在控制器中有两个函数(删除和跟踪链接)时,删除函数将删除。但以下链接将在变量中回显数据,但不会在数据库中写入。您的控制器将使用auth组件,当且仅当您直接从控制器调用它时,尝试为$user\u id添加一些静态值,它将工作,并且您将了解您的问题它仍然不工作。我把静态值放在$user_idHey thanx中,它起作用了。是Auth造成了这个问题。无论如何,谢谢你的帮助。:)