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
Related
I'm trying to figure out how to inject a dependency into a class in Laravel.
My structure:
SimpleController extends BaseController
{
public function example(SimpleModel $model, SimpleValidationRequest $request)
{
$result = $model->doStuff()
return $this->makeResponse($result);
}
}
SimpleModel extends Model
{
public function doStuff(ComplexService $service)
{
$service->doComplexLogic($this);
}
}
I have registered the ComplexService in my own service provider:
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(ComplexService::class);
}
}
I want to inject the service straight into the simpleModel's doStuff method, without having to inject it into the controller and then into the model. We're busy moving a monolithic application to Laravel and have service classes that contain all the complex business logic. Much of the logic is shared between different classes, so a controller method might call a model that calls a service that ends up making 4 or 5 calls to other services, and I want to be able to inject another service into any method that needs it without having to send it down from the controller all the way through to the bottom method that might need it.
Is there a way to do this? I have been looking online but everything I've found has required me to inject the service into the controller and then sending it through the application from there, which I want to avoid.
You can simply call the singleton inside the method via the app() helper
SimpleModel extends Model
{
public function doStuff(ComplexService $service)
{
app()->singleton(ComplexService::class)->doComplexLogic($this);
}
}
or recover the singleton by injecting it in the model via it's __construct() method.
In the Laravel documentation, I found the following - https://laravel.com/docs/5.4/container#the-make-method
but I am still confused as to what exactly the make() method does. I know the create() method uses the make() method and then persists them into the database, so does make() methods just temporarily save it in php tinker or something? Sorry, I'm Laravel noob. I'm trying to figure out these functions. Thank you! :)
The make method will return an instance of the class or interface you request.
Where you request to make an interface, Laravel will lookup a binding for that interface to a concrete class.
E.g.
$app->make('App\Services\MyService'); // new \App\Services\MyService.
One advantage to using the make method, is that Laravel will automatically inject any dependencies the class may define in it's constructor.
E.g. an instance of the Mailer class would be automatically injected here.
namespace App\Services;
use \Illuminate\Mail\Mailer;
class MyService
{
public function __construct(Mailer $mailer) {
$this->mailer = new Mailer;
}
}
I discovered recently that when you use make (), you are installing the class and you can access the methods of that class or model, this is a useful for the Test and validate that you are getting what you want Example:
User model
class User extends Authenticatable
{
public function getRouteKeyName ()
{
return 'name';
}
}
Test user
class UserTest extends TestCase
{
public function route_key_name_is_set_to_name ()
{
$ user = factory (User :: class) -> make ();
$ this-> assertEquals ('name', $ user-> getRouteKeyName ());
// When you access the getRouteKeyName method you get the return, that is 'name'
}
}
On the other hand if you use "create" that would give an error because you are creating a user
I'm using https://github.com/artesaos/seotools package for seo.
I am trying to override getCanonical function located at https://github.com/artesaos/seotools/blob/master/src/SEOTools/SEOMeta.php and make it's output as lowercase. Could you please guide me how can I do this?
You can try following :
Step 1:
Create a child class extending SEOMeta class and override the getCanonical function.
Class XyzSEOMeta extends SEOMeta {
public function getCanonical () {
// Write your logic here
}
}
Step 2:
Create the Service Provider for overridden class. First parameter of bind function must be same as facade accessor of SEOMeta Facade (check here). Register this facade in config/app.php after the service provider of seotools package. :
Class XyzSEOMetaServiceProvider extends ServiceProvider {
public function register(){
$this->app->bind('seotools.metatags', function(){
return new XyzSEOMeta($this->app['config']);
})
}
}
You are all set. Hope this will help.
EDIT:
Above mention method will just override the single class. If you want to change the logic of more than one class. Best way is to fork the project. Change the code and push it to your fork. Use forked project as your composer dependency. Follow the link to know how to use private repository as composer dependency : https://getcomposer.org/doc/05-repositories.md#using-private-repositories
It's very simple just like we overriding any parent class function in derived class.
Create your own class and extend your package class SEOMeta and re-declare function that you want to override and put your logic inside. Don't forget to use namespace of your package class SEOMeta at upper your custom class.
Now use your custom class instead of package class inside your controller.
e.g
use Path\to\SEOMeta;
class urclassname extends SEOMeta{
public function overridemethodname(){
// put ur logic here.
}
}
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
I'm developing an application, where the same function I'm writing for some controllers. So I wanted one common function for all controllers. Please help me out I've stucked up with this. The work would be more appreciated.
You can develop a helper or library.
codeigniter version 2 : https://codeigniter.com/userguide2/general/creating_libraries.html
codeigniter version 3 : https://codeigniter.com/user_guide/general/creating_libraries.html?highlight=library
You can create MY_Controller class in application/core
Class MY_Controller Extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function common(){
// code here
}
}
Now extend every class with MY_Controller and the function
will be available for each class
Class Test Extends MY_Controller{
public function __construct(){
parent::__construct();
}
}
When in the url you call
http://localhost/app/test/common
This will work.
create a model class and create that all function after that extend that model all model classes .you can use one function different controller.other wise you can use Cms helper.this is the best way call a common function in different controller.