Tell me how to pass the $ site_url parameter from public function actionNews to public function actionsOneNews for further content parsing. Parsing the list of news, writing them to the database, then you need to pull out the address of each news and pass it on to further parse the content. Controller:

public function actionNews() { $arr = ParseAllNews::newsList(); foreach ($arr as $key => $value){ $model = new ParseAllNews(); $model->img = $arr[$key]["img"]; $model->text = $arr[$key]["text"]; $model->title = $arr[$key]["title"]; $model->url = $arr[$key]["site_url"]; $model->save(); } } public function actionsOneNews($site_url) { $news = Content::OneNews($site_url); foreach ($news as $key => $value){ $model = new Content(); $model->img = $news[$key]["img"]; $model->text = $news[$key]["text"]; $model->title = $news[$key]["title"]; $model->save(); } } 

Model ParseAllNews:

 public static function tableName() { return 'news'; } public static function newsList(){ $site = "http://site.ru/"; $client = new Client(); $res = $client->request('GET', 'http://site.ru'); $body = $res->getBody(); $document = \phpQuery::newDocumentHTML($body); $links = $document->find('.bordered-title'); foreach ($links as $link) { $link = pq($link); $a = $link->find('a', 0)->attr('href'); $site_url = $site . $a; $get = $client->request('GET', $site_url); $body_get = $get->getBody(); $document = \phpQuery::newDocumentHTML($body_get); $links_get = $document->find('.b-longgrid-column .item.article'); $links_get->find('.item__info')->remove(); foreach ($links_get as $link_get){ $img = pq($link_get)->find('img')->attr('src'); $text = pq($link_get)->find('.rightcol')->html(); $title = pq($link_get)->find('.titles a>span')->html(); $url = pq($link_get)->find('.titles h3 a')->attr('href'); $site_url = $site . $url; $i++; $mas[$i] = array('title' => $title, 'img' => $img, 'text' => $text, 'site_url' => $site_url, ); } } return $mas; } 

Content Model:

 public static function tableName() { return 'content'; } public function getNews() { return $this->hasOne(News::className(), ['id' => 'news_id']); } public static function OneNews($site_url){ // создаем экземпляр класса $client = new Client(); // отправляем запрос к странице Яндекса $res = $client->request('GET', $site_url); // получаем данные между открывающим и закрывающим тегами body $body = $res->getBody(); // подключаем phpQuery $document = \phpQuery::newDocumentHTML($body); // получаем список новостей $news = $document->find('.b-topic'); // выполняем проход циклом по списку foreach ($news as $article) { //pq аналог $ в jQuery $article = pq($article); $img = $article->find('img')->attr('src'); $text = $article->find('.b-text')->html(); $title = $article->find('.b-topic__title')->html(); $rightcol = $article->find('.b-topic__rightcol')->html(); $i++; $mas[$i] = array('title' => $title, 'img' => $img, 'text' => $text, 'rightcol' => $rightcol); } return $mas; } 

    2 answers 2

     class SiteController extends Controller{ /* * В классе контроллера определяешь свойство: */ private $_siteUrl; // сюда будет записан url public function actionNews(){ //.... /* * Записываешь нужное значение в свойство. */ $this->_siteUrl = $model->url; // или откуда берется адрес //.... } public function actionsOneNews(){ // .... /* * Используешь полученное значение как душе угодно. */ if($this->_siteUrl) returt "Hello News!"; // .... } } 

    Ie defined a property in a class, in one method I assigned a value to it, in the other I read this value.

    • The solution is good, but my parameter is an array of links and when I write as you say it gives an error. That parameter must be a string or an object. 'The parameter class is an object' - ASYOU
    • I don’t know if it’s necessary or to launch two or two functions one by one in order to pass a parameter by one value and content parsing occurred immediately after parsing the article or otherwise return it. - ASYOU
    • If the parser accepts the link as a string, then you can: a. take an array of links and run the parser in a loop, collecting a new variable with the results of parsing, and b. specify the index in the array: $ this -> _ siteUrl [0]. - Skit

    Do return $ site_url from public function actionNews then

     $a = new actionNews(); actionsOneNews($a); 
    • What did they not quite understand where to create an instance of the class in the same controller? and actionNews is a function, not a class or did you mean by it newControllerName where does it lie? - ASYOU