3

I need to get the page source of Yii2 web page, which requires authorization.

For example, there are some posts like mysite.pro/post/123, these pages are available only for logged users. I created a view that can show posts in an editor, so I want to get the code of the post and try to get it with:

file_get_contents('http://mysite.pro/post/123',false), 

However, the server returns me a 403 code.

Is there a way to get the page as a current Yii2 user?

Teo
  • 230
  • 3
  • 14
  • 1
    show the related controller action code .. with behaviors access rules too – ScaisEdge Jan 19 '17 at 07:25
  • Sorry, but no( I mean, what is authority? Set of http request params and ip. There should be a way to do this! Thanks for your answer, I have marked it as a usefull, but it is not a solution – Teo Jan 24 '17 at 07:55
  • Well, you have said what's wrong with my answer. :) – Gynteniuxas Jan 24 '17 at 12:51

1 Answers1

1

It is forbidden due to security. But it's possible to display specific content from different file. This is why we have renderPartial thing. A quick example:

view_file.php:

<?php
// ...
echo Yii::$app->controller->renderPartial('special_content');

special_content.php:

<?php

if (!Yii::$app->user->isGuest) {
    echo "I'm visible only to registered users. ";
} else {
    echo "I'm visible only to guests. ";
}

echo "I'm visible to both types. ";

If user is logged in, then he will see: I'm visible only to registered users. And I'm visible to both types. Otherwise: I'm visible only to guests. And I'm visible to both types.

Such thing is good since it gives flexibility how you want to display content from another file.

Quick note: you shouldn't be using like $this->title in your special_content.php because it will overwrite your view_file.php.

Gynteniuxas
  • 7,035
  • 18
  • 38
  • 54