I have a function within an AdminController for sending emails. I want to access this within another controller. Can anyone advise how I would modify this to work?
OrdersController
public function postOrder()
{
$order = New Order;
...
$order->save();
// email order (call function in other controller)
$this->emailOrder($order);
}
AdminController
public function emailOrder($order)
{
//email processing goes here
}
You would strip it out, either into an abstract controller, that your controllers inherit from:
class AdminController extends MyController
Or to a service that you can call from your controller:
Mail::sendOrder($order)
Related
I'm curious is that possible to pass (as type-hinted) of custom class that extends FormRequest to be passed in to action within GET request ?
for example:
at routes/api.php
Route::get('/schema', '\App\Http\Controllers\TestController#getSchema');
then I have App\Http\Requests\SchemaRequest.php
and at controller, I want get this request from route within GET method.
class TestController extends Controller {
public function getSchema(\App\Http\Requests\SchemaRequest $request) {
// do other stuff here
}
}
I've tried to look deeper and doing some hack but nothing success yet?
Is that possible?
Any input would be appreciated, and thanks for reading
How about this workaround?
Route::get('/schema', 'TestController#getSchema',namespace =>'App\Http\Controller');
class TestController extends Controller {
public function getSchema(Request $request) {
// do other stuff here
}
}
To maintain my Laravel application and save myself from a lot of duplicate code I have made the following solution:
BaseController
class BaseController extends Controller
{
public function get($id){
return $this->baseService->get($id);
}
public function getAll(){
return $this->baseService->getAll();
}
}
BaseService
class BaseService
{
protected $model;
public function __construct($model){
$this->model = $model;
}
public function get($id){
return response()->json($this->model->where('id', $id)->first());
}
public function getAll()
{
return $this->model->get();
}
}
MyController
class MyController extends BaseController
{
protected $model;
protected $baseService;
public function __construct(){
$this->model= new Model();
$this->baseService = new BaseService($this->model);
}
/**
* This controller has all the functionality from BaseController now
*/
}
What I'm wondering if this is a good method. Should I stick with this or should I use a different approach? I've heard about Traits but not sure if they are doing the same thing. It's Laravel 5.5 I'm using.
Yes, traits are used to move methods out of a controller regularly. A good example that the Laravel framework uses is the ThrottlesLogin trait. Take a look at https://github.com/laravel/framework/blob/5.5/src/Illuminate/Foundation/Auth/ThrottlesLogins.php#L20
to see how the methods are moved outside of a controller but can be still accessed by importing the trait using the use keyword.
While traits would work for your use case I wouldn't use them here for the functionality you are looking for. I would use the repository pattern. It would better separate your code and make it more reusable.
Take a look at https://bosnadev.com/2015/03/07/using-repository-pattern-in-laravel-5/ for more information on the repository pattern. Basically, you would separate your code into a separate repository and use Laravel's built in IoC to inject the repository into your controller.
MyController
class MyController extends Controller
{
protected $repo;
public function __construct(MyRepository $myRepository)
{
$this->repo = $myRepository;
}
public function index()
{
$myStuff = $this->repo->all();
}
// you can also inject the repository directly in the controller
// actions.
// look at https://laravel.com/docs/5.5/controllers#dependency-injection-and-controllers
public function other(MyRepository $repo)
{
$myStuff = $repo->all();
}
}
This is the perfect use case for a Trait. Traits are intended for reusable functions. They're super simple to implement, and won't take more than a few minutes to change what you have.
Here is a great article on them: https://www.conetix.com.au/blog/simple-guide-using-traits-laravel-5
im trying to create a base controller for some controllers.
Im extending MY_Controller from CI_Controller on application/core. I have a folder named "ajax" in application controllers and i pretend to create a parent class and extend from ajax/classes. Extending all from MY_Controller.
Some like this
class Ajax extends MY_Controller{
public function __construct(){
blablabla
}
}
class Ajax extends MY_AJAX{
public function __construct(){
blablabla
}
}
class user extends Ajax{
blablabla
}
Is it necessary to use includes, load_class, etc. or are there another "legal" way to do that?
Regards!
SOLVED YET:
How to create two parent controllers in Codeigniter?
This works for me:
class MY_Ajax extends MY_Controller{
public function __construct(){
CI_Controller::__construct();
}
}
I want to call upload_controllers methods in admin controller. I've attached my code here. But its not working. Rather its shows Unable to locate the specified class: Session.php
class Admin_controller extends CI_Controller {
public function __construct() {
parent::__construct();
this->load->helper('url');
}
function index(){
require_once(APPPATH.'controllers/uploads_controller.php');
$testObj = new uploads_Controller();
$testObj->index();
}
}
this is my controller: common :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Common extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test(){
echo 1;
}
}
and this the second controller (register):
class Register extends Common {
public function __construct() {
parent::__construct();
}
public function user_registration(){
$this->test();
}
}
when i access the user_registration function it shows me this error:
Fatal error: Class 'Common' not found in /home/attilana/domains/attila-naghi.com/public_html/application/controllers/register.php on line 3
How do i access the test() function from the class common , in the user_registration function from the class register?
In Codeigniter calling a controller from another controller even in an example like this is not possible because of the situation that it doesn't allow multiple controller instances in a request. Because of this situation you have three main options actually.
First, you can extend Codeigniter's base controller, CI_Controller, with MY_ prefix so that Codeigniter will recognize it as a common base class. The main idea here is to write the methods that will be common in most of your controllers.
If we take a look at your example and try to adapt it as mentioned above :
Initially, you need to extend the CI_Controller class:
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test(){
echo 1;
}
}
Secondly, create your register controller as below:
class Register extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function user_registration(){
$this->test();
}
}
Another possibility is you can create a helper and call that function globally or you can create a library and load it within your controller and call that.
For further information, you can read from here: http://ellislab.com/codeigniter/user-guide/general/core_classes.html or from old docs: https://github.com/EllisLab/CodeIgniter/wiki/MY-Controller