Controller 1:
namespace App\Http\Controllers;
class HomeController extends Controller
Controller 2:
namespace App\Http\Controllers\Games;
class ExchangeController extends Controller
i have problem when using function in controller, in 1st controller on same level as Controller is extended then $this->rollback method works, but with 2nd controller i cant access it but still can access If I rename the function, why can't I call the function named rollback in case 2 but can use in case 1
Related
In my laravel application i should use some create and update method of some controller in another controller
According to my search is not a good thing to call a method from controller in another
I cant see the why don't call a controller method in another controller
I'm doing this way :
class Controller extends BaseController
{
protected $variable;
public function __construct()
{
$this->variable = "Hello";
}
}
and
class ClientController extends Controller
{
public function __construct()
{
parent::__construct();
}
}
The __constructor is a magic method of class. It calls when you trying to create instance of class. So there is no way to use constructor without creating an instance or extendeding from another class. If you have a common code in different classes there a best way to use traits. thats give you an opportunity to include your trait and use methods ,making your code beutiful , flexible , readable following principes DRY,KISS.
you can create a base class with constructor and extend other controller of it
or you can put your code in to Http\Controllers\controller.php ('main controllers constructor')
also you can use trait
Model included in my Controller as
use App\Entities\OrignalTripModel;
But it returns
Class 'App\Entities\OrignalTripModel' not found
i am using it into controller as
OrignalTripModel::create($inputs);
Kindly include it in your controller like this:
use App\Entities\OrignalTripModel;
And please check namespace for the model OriginalTripModel to see if it has namespace App\Entities; and that is extends model like this class OriginalTripModel extends Model
I was trying to grab the current path that was asked by the user.
Following the doc I have created these 2 methods:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use GuzzleHttp\Client;
class TestController extends Controller
{
public function reqTest1(Request $req) {
dd($req->path());
}
public function reqTest2() {
dd(Request::path());
}
}
The reqTest1 works good, but I had to inject the Request object inside the function. There I can grab path() method, and I have the response as expected.
But if I dont wan to inject dependencies I should be able to get the same method only calling the static class Request. (I dont know).
I just realise that we dont have the same methods available in these 2 scenarios.
The methods list for the injected object is much bigger than the static class that generate the same object, for the Request class..
So, why Request class has different methods from a Request object created by a dependency injection?
A Laravel facade is a class which provides a static-like interface to services inside the container, i.e. every method you call statically on the Request facade is rerouted to the actual request object. This is the reason the Request facade is a object with few methods itself.
If you want to call the facade in your code change this line:
dd(Request::path());
to this:
dd(\Request::path());
or you are calling the static path() function on Illuminate\Http\Request not the Request facade, because of this line:
use Illuminate\Http\Request;
In my view, it's better to use the helper function request() instead of the \Request facade.
Method validate does not exit. I tried to validate my form values as shown in the figure. But it give me this error. Please tell me . I am new to laravel.
[
You aren't extending the correct Controller.
class UserController extends Controller {
...
}
Controller then extends BaseController.
If you open up Controller which resides in the same namespace as your other controllers, you will see it uses the trait ValidatesRequests which is what provides the validate method.
You can also remove the line use Illuminate\Routing\Controller as BaseController;. There should be no reason to import that.
I am learning CodeIgniter. I have defined a MY_Controller Class residing inside Application/Core Folder.
Inside this, there is a public function.
My question is, Can I directly access this function or any other public function defined in My_Controller via the browser url.
Yes it it Possible.
If the method is public you can simply call it using any controller that extends My_controller and does not override the method ( i.e does not have a method with the same name as that defined in the My_Controller class)
like so :
This is how you can do it. Create a simple controller which will extend My_Controller :
someController.php
class someController extends MY_Controller{
}
now you can access it from url as such :
yourdomain.com/someController/yourMyControllerMethodName
You can call it from browser URL.
So that inside your controller you should define index() function. Because when it calls to your controller and if there is no any method calling with it, CodeIgniter automatically call index() function.
So
www.myproject.com/contact - this will call index() function of contact controller
www.myproject.com/contact/branch - this will call method inside contact controller