Skip to content
Sep 20 11

Twig extensions – name them correctly!

by rich

Came across this one today whilst integrating some new code into my current Symfony2 project. The project has 2 custom Twig extensions – one with some filters in, one with some custom methods. When creating the latter, I’d copied the class from the former and tweaked to suit. In doing so, I’d left these lines in:

    /**
     * Name of this extension
     *
     * @return string
     */
    public function getName()
    {
        return "myfirstextension";
    }

The Twig documentation states that all extensions should be named uniquely, unless you need to replace any existing extensions, in which case you should name them the same. See here for a better explanation :-) Great for extensibility and adjusting extensions that maybe you don’t have control over. Not so good when both extensions do something different and you don’t quite understand why it’s not working….

Anyway, quick question in the #symfony IRC channel and Stof came to the rescue. Give that man a medal :-) and name your extensions uniquely if you don’t want them to overwrite others!

Aug 25 11

Logging out a user programmatically in Symfony2

by rich

Quick post this, as I know I’ll forget/lose this code otherwise… Sometimes you need the ability to force a logout of a user. The example I’ll use here is a user requesting their account be deactivated – you assume that upon confirmation of the deactivation the user’s model is marked as such, and the user is subsequently logged out and shown a confirmation page:

// Update your model here. Example:
$user = clone $this->get("security.context")->getToken()->getUser();
$user->setEnabled(false);
$user->save();
 
// Log the user out
$this->get("request")->getSession()->invalidate();
$this->get("security.context")->setToken(null);
 
// Flash var to enable the user to see the deactivation success page
$this->get("session")->setFlash('deactivate_account.success', true);
 
// and redirect to success page
return new RedirectResponse($this->get("router")->generate("deactivate_account_success"));

Your deactivation page should allow viewing without requiring authentication (anonymous users can view it). The above code should be self-explanatory – invalidate the current session, and set the security token to null (rather than the previously-authenticated token). The flash variable I set so that viewing of the success page is one-shot rather than being permanently viewable. Just my preference :-)

Aug 6 11

Greyscale printing on a Canon MP270 in Linux

by rich

Just came across this article on ubuntuforums.org which describes how to add a greyscale printing option when using a Canon MP270 printer under Ubuntu. Reprinting here in case the original disappears. Dead simple!:

After reading the manual from Canon, I have made the grayscale printing working too. For those who need it, here is the fast tip:

Under System–>Administration–>Printing

right click MP250 that you have already setup, then select Properties from the popup menu, go to Job Options, scroll down to the bottom, add this option in:

CNGrayscale

click the Add button to add the option to the list.

Beside the CNGrayscale option, type in TRUE in the textfield, then click Apply, and close the Properties dialog.

By doing this, this printer is setup to be Grayscale printing by default. You can definitely have another copy of printer setup, pointing to the same printer, but for color printing. This is what i have done.