disable layout for sfDoctrineGuardPlugin in Symfony - php

How could I prevent mentioned plugin's login form from using default layout? I am aware of this question, but that answer doesnt work for me. For starters, there's no signin module in modules dir, probably plugins handle it in different way, I dont know. Just learning symfony. Thanks in advance :)

For now its not possible to set custom layout for some sfGuardAuth action via custom view.yml.
This is how I did it.
This is my apps/backend/modules/sfGuardAuth/actions/actions.class.php:
<?php
require_once(sfConfig::get('sf_plugins_dir').'/sfDoctrineGuardPlugin/modules/sfGuardAuth/lib/BasesfGuardAuthActions.class.php');
class sfGuardAuthActions extends BasesfGuardAuthActions
{
public function preExecute()
{
$layout = $this->getActionName() == sfConfig::get('sf_login_action') ? 'sfGuardLayout' : $this->getLayout();
$this->setLayout($layout);
}
}

If you just want to set a different layout, you need to add a module (just create it manually) called "sfGuardAuth". Inside the /config/ directory for that, change the layout in the view.yml like for any other module. This is explained in:
http://www.symfony-project.org/plugins/sfDoctrineGuardPlugin/4_0_0
... under section "Customize sfGuardAuth module actions".
However, if you want to "embed" your login form on another existing page, you could turn the login into a component - which means it uses the existing layout of the page it occurs in.
Component action in a custom module:
public function executeSigninLightbox(sfWebRequest $request)
{
$class = sfConfig::get('app_sf_guard_plugin_signin_form', 'sfGuardFormSignin');
$this->form = new $class();
}
... which like all components uses a partial as its view. The partial now has access to $form like a standard login page. The partial for this would be called "_signinLightbox".
Hope that helps.

Related

Change View Layout Based on Device

I have a plugin to detect mobile view or desktop view. I need to be able to change my view layout based on either the user is view from a mobile device or desktop.
I tried to control the logic in Zend_Controller_Plugin Abstract Class in dispatchLoopStartUp() method and also in the bootstrap method _initLayoutName() but I still couldn't achieve the result.
Find the below snippet of what i expected to achieve
public function checkDetectDevice() {
$detect = new My_MobileDetect();
if($device->isMobile()) {
//Change View Layout.
}
}
Can someone help me on how to achieve view layout changing in Zend v1
To change the layout in a plugin, try this:
Zend_Layout::getMvcInstance()->setLayout('your_layout'); // for your_layout.phtml

Zend same template for different modules

I am developing a system which has multiple user levels. However most of the time the views inside each modules should be the same.
For example a user view should be 99% identical to the admin view however an admin can have some small extras like delete buttons on user posts etc.
What is the best approach to not duplicate a ton of template view files within each module?
The best solution I can think of is using the _base module and putting the view files in there and inside them do an (if($user->isAdmin(): extra HTML bits) and have both the user module and admin module render the base module views?
If you are creating template file(layout.phtml) in your /module/Application/View/layout/ folder, then the layout will be applied for all the views.
If you want to disable layout for some specific view, you can use like this:
public function yourAction() {
$viewModel = new ViewModel(array(
'foo' => 'bar'
));
$viewModel->setTerminal(true);
return $viewModel;
}
Very first need to check if user is admin then in view you can call another view partially having extra code like delete button using $this->partial() helper.

Make redirect in Zend Framework 2 without template

I'm trying to make a redirect from route /admin to /admin/post/list.
I set a route from /admin to IndexController::indexAction()
Then I made controller like this
class IndexController extends AbstractActionController
{
public function indexAction()
{
$this->redirect()->toRoute('postList');
}
}
It works well, but ZF2 required to make a template index/index.phtml.
How I can do this redirect better, without empty templates?
If you add return it should work:
return this->redirect()->toRoute('postList');
I found initially that I couldn't make the return response option work at all, in spite of the docs and plenty of abortive attempts.
In the end I stripped out the default application module that I had included to bootstrap things "out of the box" but wasn't using for anything else, and after shifting the translator config and the factory for it, (as required by the error template - I suppose i could have removed even that, as not required) it started working.
Hey presto!

Use specific layout for a model

Can anyone tell me how can I define and use a specific layout for a model (not a template)? I would like to do this for my custom 404 error page.
As Peter Bailey commented above, your layout is a component of the view and has nothing to do with models. Therefore you'd be able to do something like this in the actions module you're using (normally default):
public function executeError404(sfWebRequest $request)
{
$this->setLayout("your_layout_name");
// ...
}
and then in your [APPNAME]/templates directory, create the your_layout_name.php template file as you would with any other template.

Making static pages in Symfony 1

Im new to symfony and have some simple questions. I am trying to understand the module system, but I dont understand how I create the actual homepage or other pages that are not based off of a model from the db. For example, the simple about page that has static info or the homepage that is a combination of a bunch of information from different models.
Can anyone help?
First of all, modules do not have to be restricted to a model from the database. You can have a Foo module which relies on no database content, and a Bar module that is primarily based on 3 different models. The module separation is a way to logically break up your site into manageable sections. Eg an e-commerce site might have a Products module, a Categories module and a Cart module and so on.
Your last sentence can then be split into 2 parts:
1) Static information can be on any page - if it's for things like "About us" and "FAQ" etc, I personally tend to use a "default" or "home" module, and create the various actions in there vis:
./symfony generate:module appname home
and
class homeActions extends sfActions
{
public function executeAbout(sfWebRequest $request)
{
// ...
}
public function executeFaq(sfWebRequest $request)
{
// ...
}
}
with the corresponding template files (aboutSuccess.php, faqSuccess.php).
2) A page can be comprised of data from many different models - just use your preferred ORM's method of retrieving data and set it to the view ($this->data = MyModel->findByColumn(...) etc). If you mean data from different modules, then you'd probably be better off looking at partials or components for elements of a page that can be used across different modules (navigation etc). See the Symfony docs for more details on these.
I'm used to handle static pages in this way.
First I create a new entry in apps/frontend/config/routing.yml:
page:
url: pages/:page
param: { module: page, action: index }
Then I write a "page" module (apps/frontend/modules/page/actions/actions.class.php):
<?php
class pageActions extends sfActions
{
public function executeIndex()
{
$this->page = $this->getRequestParameter("page");
$this->forward404Unless($this->_partialExists($this->page));
}
protected function _partialExists($name)
{
$directory = $this->getContext()->getModuleDirectory();
return (is_readable($directory.DIRECTORY_SEPARATOR."templates".
DIRECTORY_SEPARATOR."_".$name.".php"));
}
}
Last step, put in modules/page/templates/indexSuccess.php this code:
<?php include_partial($page); ?>
So all you have to do from now is to create a partial for each static page ie.
apps/frontend/modules/page/templates/_home.php which you can reach at
http://yousite/pages/home (without the need to add a new routing entry for every page)
You can create a module, e.g. called static and create actions for every static page or only one action that delivers the page depending on a request variable. The only thing this action does is loading a template.
IMHO it would be good if symfony comes with a default module for this.
For example actions of (my custom) module static:
class staticActions extends sfActions
{
public function executeIndex(sfWebRequest $request)
{
if(!$request->hasParameter('site')) {
return sfView::ERROR;
}
$this->site = $request->getParameter('site');
}
}
With this template:
//indexSuccess.php
<?php include_partial($site) ?>
The actual statics sites are all partials.
In my routing.yml looks like this:
# static stuff
about:
url: /about
param: {module: static, action: index, site: about}
This way you only have to create a new partial and a new routing entry when you add a static site and you don't have to touch the PHP code.
Another way to serve static pages without having to write any controller code is to set up the route something like the following:
myStaticPage:
pattern: /pageName
defaults:
_controller: FrameworkBundle:Template:template
template: MyBundle:Home:pageName.html.twig
Then just create your twig template and it should work fine.
Apart from the above, consider having a CMS for static pages, so you won't need technical savy people to mantain them or change them. This depends on the project, of course.
For really static and independent pages you can simply create any file in [pathToYourProjectRoot]/web directory.
It may by i.e. [pathToYourProjectRoot]/web/assets/static_html/about.html.
Then link to the page directly by http://your.site.com/assets/static_html/about.html.

Categories