Wednesday, 9 July 2014

Laravel 4: Create multi language page with session

Laravel is a strong PHP framework that is widely used. Recently, I was developing with that framework and I needed to create a website with content in 2 languages: catalan and spanish.

To solve this problem you can use some options... like url with language or save language in session. I decided to use the last one.

I created this code:

filters.php

App::before(function($request) {
// We use the locale of the session
if ( Session::has('locale') ) { App::setLocale(Session::get('locale')); }
});

BaseController.php

class BaseController extends Controller {

public function switchLocale($lang) {
// We change the locale lang in the session and redirect back (see filter language in filters.php).
if ( in_array($lang, Config::get('app.available_language')) ) {
Session::put('locale', $lang);
}
return Redirect::back();
}
}

app.php

'locale' => 'es',
'fallback_locale' => 'ca',
'available_language' => array('es', 'ca'),

Finally,  in our view we use this code in the links used to change the language:

Catalan -> URL::action('BaseController@switchLocale', array('ca'))
Spanish -> URL::action('BaseController@switchLocale', array('es'))

Wednesday, 4 June 2014

felogin: actions to do after login

Sometimes inside a TYPO3 website after install and config felogin, we would like to do some actions everytime that user would acces to website. To do that, we can use an extension hook. In this example we use TYPO3 4.5.32 and 1.3.1 felogin.

Inside ext_localconf.php of our extension:

$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['felogin']['login_confirmed'][] = 'EXT:test_extension/hooks/class.feloginhook.php:test_extension_hooksHandler->onLogin';

Inside class.feloginhook.php:

class test_extension_hooksHandler { public function onLogin() { // Actions to do here... Insert in a database, in a log... redirections... } }

Regards!

Thursday, 15 May 2014

jQuery: Send form by ajax

Hello,

Sometimes we want to send form data but we don't want to reload page. In this case best option is send data by ajax and jQuery. In this example we used version 1.10.2 of jQuery.

HTML code:

<form id="FORMID" accept-charset="UTF-8" action="action.php" method="POST"> <input type="text" name="username" id="username" placeholder="Write username"> </form>;

Javascript code:

$('#FORMID').on('submit', function(e)  {
$.post(
$(this).prop('action'),
{
"username": $(this).find('input[name=username]').val()
},
function(data) {
alert(data.result);
},
'json'
); return false;
});


php (action.php) code:

<?php
$username = $_POST['username'];
$ret = array();
$ret['result'] = 'OK';
return json_encode($ret);

Best regards!

Thursday, 10 April 2014

HMENU: add class to li tag

Hello,

Today I was creating a new HMENU with Typoscript and I have found a problem. I had this HMENU already configured:

temp.links = HMENU temp.links.special = list temp.links.special.value = 1,2,3,4,5 temp.links { 1 = TMENU 1 { wrap = <ul>|</ul> NO.linkWrap = <li>|</li> } }

Then I saw that I wanted to add a class in the last li tag. The solution is:

temp.links = HMENU temp.links.special = list temp.links.special.value = 1,2,3,4,5 temp.links { 1 = TMENU 1 { wrap = <ul>|</ul> NO.linkWrap = <li>|</li>|*||*|<li class="specialClasss">|</li> } }

Best regards!

Monday, 31 March 2014

TYPO3: Use session variables

Hello,

In TYPO3 is very useful to manage session variables in frontend plugins:
  • Get 'test' variable: $test = $GLOBALS['TSFE']->fe_user->getKey('ses', 'test');
  • Save  'test' variable: $GLOBALS['TSFE']->fe_user->setKey('ses', 'test', $test); $GLOBALS['TSFE']->fe_user->storeSessionData();
Regards!