I am working on laravel 6. I have an controller ABController which extends AbBaseController.
Now AbBaseController extends laravel's Controller which we do normally in every controller.
I have a method called index in ABController
eg
Class ABController extends AbBaseController {
public function index() {
// some index code
}
}
Now i want to use index method using router.php file as
Router::get('/index', 'ABController#index');
While debugging route worked as
Router::get('/index', function(){ echo 'in'; });
i get proper output.. But when controller is replaced, ABController is not getting called.
Can any body help me out . Is there any way to deal with it.
Related
I am using Codeigniter framework to develop a website. I am currently working on home.php view under the view folder. I need to use UserInfo() function which is inside one of the controllers. Any suggestion how to access that function?
class Welcome extends CI_Controller {
public function UserInfo(){
$this->load->model('model_user');
$data['title'] = 'Users';
$data['users'] = $this->model_user->getUser();
$this->load->view('template/users', $data);
}
}
You cant call controller method inside another controller. Its No Way to do it.
You have two way to resolve this issue
If you want to access the function which place inside the
controller, add that into an model. So by loading model you can call
it.
use redirect('welcome/UserInfo') if you just need to call the function
As You want to call controller function in other controller.In codeigniter App folder core folder exists you make a custom core controller and all other controllers extend with your custom controller
In your Custom Core controller
class CustomCore extends CI_Controller
{
/* ---YOUR FUNCTION IN CUSTOMCORE---- */
public function mycorefunc()
{
//Do something
}
}
and your all other controllers extend with custom core
class YourController extends Customcore
{
function controllerfunction()
{
$this->mycorefunc();// Call corefunction
}
}
I have middleware that uses a redirect to call a controller which then displays a view.
public function handle($request, Closure $next)
{
redirect()->action('Full\Namespace\To\Controller\ErrorController#fourOhThree');
}
I also have this as a route. When I follow the route the view is displayed fine. When I try and redirect using action and pass the namespace of my controller, laravel tries to find the controller in the base app. I get error
Action App\Http\Controllers\Full\Namespace\To\Controller\ErrorController#fourOhThree not defined.
When the controller is located at
App\Vendor\Myname\Mypackagename\Controllers\ErrorController#fourOhThree
I have namespaced my controller correctly as far as I can tell as it matches the other namespaced controllers in this directory. This is the only controller I am trying to call from an action.
ErrorController.php within App\Vendor\Myname\Mypackagename\Controllers
namespace Full\Namespace\To\Controller;
use App\Http\Controllers\Controller;
class ErrorController extends Controller
{
public function fourOhThree()
{
return view('...');
}
}
I think I am doing something wrong in how I am passing the namespaced controller to the action method.
Try adding a '\' in front of the qualified name.
action('\Full\Namespace\To\Controller\ErrorController#fourOhThree')
i want to call the controller => function without specifying in route in Laravel 5.1.
such as controller / function
Example: admin/delete
so i want to call the above controller's function without specifying in routes is their any way to do that?
Also, if it is possible then how to pass parameters to that function?
I think you are looking for RESTful Controllers.
So in your route file you only have:
Route::controller('admin', 'AdminController');
and in your controller:
class AdminController extends BaseController {
public function show($adminId)
{
//
}
public function destroy($adminId)
{
//
}
}
I Need some advice, as I'm still a bit new to Laravel and MVC in general. I'm coding a small web application that presents some data on the page, fetched from a remote API. However, the page already has a controller to it. The other controller I will be using I'm hoping I can also reuse it for other pages. I'm pretty stuck here.
So the two controllers
HomeController.php
ApiController.php
The HomeController is the original controller, which gets the view file (home.blade.php), with some other data that's being loaded from the controller.
With the ApiController, I want to fetch the api (json) results, do some changes and then load those changes to the HomeController as well. The changes would be like an array of methods and such that's being loaded to the view.
So How can I load both controllers inside of the same view?
First of all controllers doesn't get loaded inside view instead, you should load a view from a controller and to make the remote request for an API call you don't need to use another controller but you may use it if you have other use of API and need a separate controller. The flow is something like this:
class HomeController extends BaseController {
public function index()
{
// make the api call/remote request
// modify the returned data
// load the view
}
}
Let's rewrite it:
class HomeController extends BaseController {
protected $apiService = null;
public function __construct(ApiService $apiService)
{
$this->apiService = $apiService;
}
public function index()
{
// make the api call/remote request
$apiData = $this->apiService->makeRequest();
// modify the returned data.... then...
// load the view
return View::make(...)->with('apiData', $apiData);
}
}
So, it seems clear that, you should use the API related process in a separate class as a service, maybe a model or a simple repository class and inject it to your HomeController then use it from the controller.
Do all the API stuffs in ApiService and call methods of that class from the HomeController, in this case you may implement the ApiServiceRepository as a concrete class by implementing an interface, i.e. ApiService. So, finally it could be like this:
interface ApiService {
public function makeRequest();
}
// Implement the interface in concrete class
class ApiServiceRepository implements ApiService {
public function makeRequest()
{
// $data = make remote request
// return $data
}
}
Use the class HomeController with __construct as given above and add a IoC binding like:
App::bind('ApiService', 'ApiServiceRepository');
So, you don't have to worry about the dependency injection in the constructor of your HomeController.
BTW, to use a method from another controller, for example ApiController from HomeController you may use something like this:
$apiController = App::make('ApiController');
// Call any method of "ApiController" class/object
$apidata = $apiController->makeCallToMethod();
You may also check this article for understanding the use of repository pattern in Laravel.
I have Content controller with REST methods (index..create..store..) and i want to run some code before any of those methods run.
what i am trying to do is to set var for my layout with some data that is relevant to all my methods within Content controller:
$this->layout->myvar = 'some-data';
I tried to do something like that:
class ContentController extends BaseController {
function __construct() {
$this->layout->myvar= 'some-data';
}
..
but it doesn't seems to work.
i get "Attempt to assign property of non-object" error.
Laravel 5.1+
This has been deprecated in favour of Middleware.
Laravel 4
You could set the beforeFilter like this:
class ContentController extends BaseController {
function __construct() {
// this function will run before every action in the controller
$this->beforeFilter(function()
{
// this will make the variable $myvar available in your view
$this->layout->with('myvar', 'some-data');
});
}
}
try share in app/routes.php
View::share('variable_name', 'value');
ex:
View::share('name', 'Steve');
will share variable with its value across all views