Implement action to serve static pages #2932

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

Comments

Projects
None yet
6 participants
@mikehaertl
Contributor

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

This comment has been minimized.

Show comment
Hide comment
@samdark

samdark Mar 31, 2014

Member

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);
  }
}
Member

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

This comment has been minimized.

Show comment
Hide comment
@mikehaertl

mikehaertl Mar 31, 2014

Contributor

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

Contributor

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

This comment has been minimized.

Show comment
Hide comment
@samdark

samdark Mar 31, 2014

Member

Will do.

Member

samdark commented Mar 31, 2014

Will do.

@qiangxue

This comment has been minimized.

Show comment
Hide comment
@qiangxue

qiangxue Mar 31, 2014

Member

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

Member

qiangxue commented Mar 31, 2014

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

@Ragazzo

This comment has been minimized.

Show comment
Hide comment
@Ragazzo

Ragazzo Mar 31, 2014

Contributor

@qiangxue how does it check it ?

Contributor

Ragazzo commented Mar 31, 2014

@qiangxue how does it check it ?

@samdark

This comment has been minimized.

Show comment
Hide comment
@samdark

samdark Mar 31, 2014

Member

@Ragazzo To be in a certain directory.

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

Member

samdark commented Mar 31, 2014

@Ragazzo To be in a certain directory.

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

@Ragazzo

This comment has been minimized.

Show comment
Hide comment
@Ragazzo

Ragazzo Mar 31, 2014

Contributor

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

Contributor

Ragazzo commented Mar 31, 2014

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

@qiangxue

This comment has been minimized.

Show comment
Hide comment
@qiangxue

qiangxue Mar 31, 2014

Member

Fine with me adding it as an action.

Member

qiangxue commented Mar 31, 2014

Fine with me adding it as an action.

@Ragazzo

This comment has been minimized.

Show comment
Hide comment
@Ragazzo

Ragazzo Mar 31, 2014

Contributor

#2748 can be implemented in same PR i think )

Contributor

Ragazzo commented Mar 31, 2014

#2748 can be implemented in same PR i think )

@samdark

This comment has been minimized.

Show comment
Hide comment
@samdark

samdark Mar 31, 2014

Member

OK, will implement the action.

Member

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

Merge pull request #2934 from yiisoft/action-for-static-pages
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

This comment has been minimized.

Show comment
Hide comment
@mikehaertl

mikehaertl Apr 2, 2014

Contributor

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.

Contributor

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

This comment has been minimized.

Show comment
Hide comment
@samdark

samdark Apr 2, 2014

Member

Can't it be done via parametrized route?

Member

samdark commented Apr 2, 2014

Can't it be done via parametrized route?

@mikehaertl

This comment has been minimized.

Show comment
Hide comment
@mikehaertl

mikehaertl Apr 2, 2014

Contributor

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.

Contributor

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

This comment has been minimized.

Show comment
Hide comment
@samdark

samdark Apr 2, 2014

Member

Makes sense.

Member

samdark commented Apr 2, 2014

Makes sense.

@samdark samdark reopened this Apr 2, 2014

@samdark

This comment has been minimized.

Show comment
Hide comment
Member

samdark commented Apr 2, 2014

@samdark samdark closed this Apr 2, 2014

@mikehaertl

This comment has been minimized.

Show comment
Hide comment
@mikehaertl

mikehaertl Apr 2, 2014

Contributor

Cool, thanks!

Contributor

mikehaertl commented Apr 2, 2014

Cool, thanks!

@timfayz

This comment has been minimized.

Show comment
Hide comment
@timfayz

timfayz 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 (

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

This comment has been minimized.

Show comment
Hide comment
@samdark

samdark Jun 23, 2014

Member

@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!

Member

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

This comment has been minimized.

Show comment
Hide comment
@slavcodev

slavcodev Jun 23, 2014

Contributor

@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

Contributor

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

This comment has been minimized.

Show comment
Hide comment
@timfayz

timfayz Jun 23, 2014

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

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