How to call BaseController public function from child classes in Laravel (4)? - laravel

I have a CustomController. For not to repeat myself, I defined getVars function in BaseController. I want to call getVars function from some functions in CustomController.
However, Laravel returns 404 error without any exception. What is wrong?
class BaseController extends Controller {
public function getVars($var1, $var2, $var3=Null, $var4=Null) {
return [];
}
}
class CustomController extends BaseController {
public function doBla1($var1, $var2) {
$vars = $this->getVars();
}
public function doBla2() {
$vars = $this->getVars();
}
public function doBla3() {
$vars = $this->getVars();
}
}
Sorry :( I found the reason of error. The names of doBla1 and getVars functions are same. This results in a 404 error. Sorry :(

$this is useful when you have method/function defined in same controller.
For functions inside parent controller you can use
Parent::getVars($var1, $var2)

Related

CodeIgniter controller doesn't recognize function

This is something strange.
The controller function called product just doesn't work, but any other name works. See the following code:
namespace App\Controllers;
class Shop extends BaseController {
public function index() {
return view('shop');
}
public function asdf(){
return view('product');
}
public function product(){
return view('product');
}
}
Calling the function asdf (or whatever name it is), works. But I get a 404 if I call the /shop/product.
How to explain (and hopefully solve) it?

Pass variable from one controller to another laravel 5.4

I am trying to pass the request variable from a form request to another controller (the second controller is going to have a lot of code in it so I want to use it to keep the main controller clean), but when I try and pass the variable over, nothing happens, the variable data isn't sent over.
Here is my current code:
class mainController extends Controller{
public function store(Request $request)
{
//validation
$otherClass = (new secondController)->createDBEntry($request);
}
}
class secondController extends Controller{
public function createDBEntry($request)
{
return $request;
}
}
However, nothing is passed from the $request into the secondController. If I echo something in the secondController it works no problem, so I know it's being called, but the data isn't being sent over. What am I missing here? Keep in mind I am fairly new to laravel and I am using 5.4.
Your code works just fine. You are just expecting a behavior that is never going to happen though because:
new secondController instantiates secondController
createDBEntry($request) calls the method createDBEntry passed
with $request
The returned value is stored in the variable $otherClass
And that's it! nothing more is (and will be) happening. If you want to see the value of $otherClass (and see your code working) you have to return it:
class mainController extends Controller
{
public function store(Request $request)
{
$otherClass = new secondController
$otherClass->createDBEntry($request);
return $otherClass;
}
}
class secondController extends Controller
{
public function createDBEntry($arg)
{
return $arg;
}
}

Laravel OOP: trying to call a var in a function that has been declared in the same class

i'm in my pages controller trying to call a variable in a function that has been declared in the same class, it's not working properly.
When I put the vars back in the function, all works good.
Please let me know what i'm doing wrong:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class PagesController extends Controller
{
public $Fname = 'Mobile';
public $Lname ='Store';
public function about()
{
return view('pages.about', compact('Fname', 'Lname'));
}
public function contact()
{
$Fname = 'Mobile';
$Lname ='Store';
return view('pages.contact', compact('Fname', 'Lname'));
}
}
I'm receiving an Undefined variable: Fname error.
Thanks.
Erez
first you have to look at this.
this is simple oops concept not a laravel error at all.
public function about()
{
$Fname = $this->Fname;
$Lname = $this->Lname;
return view('pages.about', compact('Fname', 'Lname'));
}
Try this in your code:
public function about()
{
$Fname=$this->Fname;
$Lname=$this->Lname;
return view('pages.about', compact('Fname', 'Lname'));
}

Creating a public function inside a controller in Laravel

I'm trying to create a public/private function inside my controller (say PostController) to tidy up certain code.
I wrote something like this:
class PostController extends BaseController
{
public function store()
{
$startdate = dateformatchange(Input::get('startdate'));
}
public function dateformatchange($date)
{
$dateString = DateTime::createFromFormat('m-d-Y', $date);
$dateNew = $dateString->format('Y/m/d');
return $dateNew;
}
}
But I'm getting some error. Call to undefined function dateformatchange()
Am I doing it wrong? please advice where I went wrong. Sorry if it is a silly mistake.
You need to do it like this:
$startdate = $this->dateformatchange(Input::get('startdate'));

Laravel 4: Reference controller object inside filter

I have a controller in Laravel 4, with a custom variable declared within it.
class SampleController extends BaseController{
public $customVariable;
}
Two questions: Is there any way I can call within a route filter:
The controller object where the filter is running at.
The custom variable from that specific controller ($customVariable).
Thanks in advance!
as per this post:
http://forums.laravel.io/viewtopic.php?pid=47380#p47380
You can only pass parameters to filters as strings.
//routes.php
Route::get('/', ['before' => 'auth.level:1', function()
{
return View::make('hello');
}]);
and
//filters.php
Route::filter('auth.level', function($level)
{
//$level is 1
});
In controllers, it would look more like this
public function __construct(){
$this->filter('before', 'someFilter:param1,param2');
}
EDIT:
Should this not suffice to your needs, you can allways define the filter inside the controller's constructor. If you need access to the current controller ($this) and it's custom fields and you have many different classes you want to have that in, you can put the filter in BaseController's constructor and extend it in all classes you need.
class SomeFancyController extends BaseController {
protected $customVariable
/**
* Instantiate a new SomeFancyController instance.
*/
public function __construct()
{
$ctrl = $this;
$this->beforeFilter(function() use ($ctrl)
{
//
// do something with $ctrl
// do something with $ctrl->customVariable;
});
}
}
EDIT 2 :
As per your new question I realised the above example had a small error - as I forgot the closure has local scope. So it's correct now I guess.
If you declare it as static in your controller, you can call it statically from outside the controller
Controller:
class SampleController extends BaseController
{
public static $customVariable = 'test';
}
Outside your controller
echo SampleController::$customVariable
use:
public function __construct()
{
$this->beforeFilter('auth', ['controller' => $this]);
}

Resources