Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement action to serve static pages #2932

Closed
mikehaertl opened this issue Mar 31, 2014 · 20 comments
Closed

Implement action to serve static pages #2932

mikehaertl opened this issue Mar 31, 2014 · 20 comments
Assignees
Milestone

Comments

@mikehaertl
Copy link
Contributor

@mikehaertl mikehaertl commented Mar 31, 2014

Is there any specific reason why there's no equivalent to CViewAction in Yii2? I found that quite convenient.

@samdark
Copy link
Member

@samdark samdark commented Mar 31, 2014

It's easy enough to implement:

public function actionPage($view = 'index')
{
  try {
    return $this->render('site/page/' . $view);
  } catch (InvalidParamException $e) {
    throw new HttpException(404);
  }
}
@mikehaertl
Copy link
Contributor Author

@mikehaertl mikehaertl commented Mar 31, 2014

Ok, looks good.. Maybe worth to add this to the guide?

@samdark samdark added the type:docs label Mar 31, 2014
@samdark samdark self-assigned this Mar 31, 2014
@samdark
Copy link
Member

@samdark samdark commented Mar 31, 2014

Will do.

@qiangxue
Copy link
Member

@qiangxue qiangxue commented Mar 31, 2014

Need to check $view to make sure it doesn't allow running arbitrary script. (The above code does.)

@Ragazzo
Copy link
Contributor

@Ragazzo Ragazzo commented Mar 31, 2014

@qiangxue how does it check it ?

@samdark
Copy link
Member

@samdark samdark commented Mar 31, 2014

@Ragazzo To be in a certain directory.

@qiangxue that's getting complex. Maybe worth adding an action?

@Ragazzo
Copy link
Contributor

@Ragazzo Ragazzo commented Mar 31, 2014

@samdark currently it does not check anything and there can be some trouble with path traversal .

@qiangxue
Copy link
Member

@qiangxue qiangxue commented Mar 31, 2014

Fine with me adding it as an action.

@Ragazzo
Copy link
Contributor

@Ragazzo Ragazzo commented Mar 31, 2014

#2748 can be implemented in same PR i think )

@samdark
Copy link
Member

@samdark samdark commented Mar 31, 2014

OK, will implement the action.

@samdark samdark closed this in 9936acf Apr 1, 2014
samdark added a commit that referenced this issue Apr 1, 2014
Fixes #2932: Added `yii\web\ViewAction` that allow you to render views based on GET parameter
@samdark samdark added this to the 2.0 Beta milestone Apr 1, 2014
@mikehaertl
Copy link
Contributor Author

@mikehaertl mikehaertl commented Apr 2, 2014

Could we also get back the defaultView option that we had in CViewAction in Yii 1? So if the action is called without a view parameter, this default view is rendered.

@samdark
Copy link
Member

@samdark samdark commented Apr 2, 2014

Can't it be done via parametrized route?

@mikehaertl
Copy link
Contributor Author

@mikehaertl mikehaertl commented Apr 2, 2014

It probably can - but you don't always want to configure routes.

Example: I'm building an extension that comes with the manual as static pages in a module. So if someone needs the docs locally, he should only have to configure the module and then be able to acces it with http://localhost?r=mymodule. I don't want to give him ugly URLs like http://localhost?r=mymodule/default/pages&view=index. And I don't want to force him to install a URL rule either.

@samdark
Copy link
Member

@samdark samdark commented Apr 2, 2014

Makes sense.

@samdark samdark reopened this Apr 2, 2014
@samdark
Copy link
Member

@samdark samdark commented Apr 2, 2014

@samdark samdark closed this Apr 2, 2014
@mikehaertl
Copy link
Contributor Author

@mikehaertl mikehaertl commented Apr 2, 2014

Cool, thanks!

@timfayz
Copy link

@timfayz timfayz commented Jun 23, 2014

I can't understand why I can't get it work! Please help.
My code:

public function actionPage($view = 'index')
{
  try {
    return $this->render('site/page/' . $view);
  } catch (InvalidParamException $e) {
    throw new HttpException(404);
  }
}

I get the Unable to resolve the request error message

My config:

        'urlManager' => [
            'showScriptName' => false,
            'enablePrettyUrl' => true,
            'rules' => [
                ''=>'site/index',
                'test'=>'site/test',
            ]
        ]
      ....

The only way to get actionPage() work is the url as follows: bla.com/site/page?view=file-in-page-folder. However $this->render('site/page/'.$view) falls and new HttpException is frowned (

@samdark
Copy link
Member

@samdark samdark commented Jun 23, 2014

@kalopsia it's extremely hard to work with new issues posted as comments to issues closed long time ago. Please create a separate issue. Thanks!

@slavcodev
Copy link
Contributor

@slavcodev slavcodev commented Jun 23, 2014

@kalopsia I think you wrong view path

return $this->render('site/page/' . $view);

try to render view <view-path>/<controller-id>/site/page/<view-param>.php

@timfayz
Copy link

@timfayz timfayz commented Jun 23, 2014

Thank you, but it still doesn't work.. I think I create new issue soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.