Reputation: 10228
I have a class like this:
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
// I need to put a if() in here
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}
// there is some other methods in here
}
As I've commented in the code above (// I need to put a if() in here
), I need to compare something in there and use the result as $redirectTo
variable's value.
But PHP doesn't let me to do that. I mean, I want something like this:
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
if ( Lang::getLocale() == 'en' ){
$lang = '/en';
} else {
$lang = '/fa';
}
protected $redirectTo = '/home'.$lang;
public function __construct()
{
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}
// there is some other methods in here
}
As I said, php doesn't let me to do this ^. How can I do that?
EDIT: I cannot put that if statement into __construct()
method. Because setLocale()
hasn't been set yet and getLocale()
always return default language (which is fa
). Anyway I need to put that if statement out of __construct()
. Any suggestion?
Upvotes: 1
Views: 66
Reputation: 54841
Add your logics to constructor:
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
protected $redirectTo;
public function __construct()
{
if ( Lang::getLocale() == 'en' ){
$lang = '/en';
} else {
$lang = '/fa';
}
$this->redirectTo = '/home'.$lang;
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}
// there is some other methods in here
}
Update:
Class code is just a definition of a class. It is not executed in any way. Only class methods can be executed when you explicitly call to them.
Class can't have anything except properties, constants or methods.
If you need to perform some actions - create class method. There's NO OTHER WAY.
If you need to set $this->redirectTo
after call of setLocale()
then either create instance of class after this call:
Lang::setLocale();
$ac = new AuthController();
Or create a special method in your class which checks and sets $this->redirectTo
:
$ac = new AuthController();
Lang::setLocale();
$ac->setRedirectTo();
Where setRedirectTo()
is something like:
public function setRedirectTo()
{
if ( Lang::getLocale() == 'en' ){
$lang = '/en';
} else {
$lang = '/fa';
}
$this->redirectTo = '/home'.$lang;
}
Upvotes: 3
Reputation: 2713
Follow this bellow :
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
protected $redirectTo;
protected $lang;
public function __construct()
{
if ( Lang::getLocale() == 'en' ){
$this->lang = '/en';
} else {
$this->lang = '/fa';
}
$this->redirectTo = '/home'.$this->lang;
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}
// there is some other methods in here
}
Upvotes: 1
Reputation: 71
you can try this one:
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
protected $lang='';
protected $redirectTo='';
public function __construct()
{
if ( Lang::getLocale() == 'en' ){
$this->$lang = '/en';
} else {
$this->$lang = '/fa';
}
$this->$redirectTo= '/home'.$lang;
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}
// there is some other methods in here
}
Upvotes: 4