You already made it but ask where to make it?
You can add it anywhere inside the app folder (perhaps in a subfolder to make it cleaner). Just make sure that both folder and filename starts with a capital, and the namespace and classname are the same
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
i have to made a class which i use in any controller so where should i create that class and how
You already made it but ask where to make it?
You can add it anywhere inside the app folder (perhaps in a subfolder to make it cleaner). Just make sure that both folder and filename starts with a capital, and the namespace and classname are the same
@Sinnbeck ok thankyou one more confusion how would i call that function in controller by using $this-> or something other?
If you can show the class you created, I can better help
But an example
$foo = new Foo();
$foo->doStuff();
@Sinnbeck no i don't created the class till now
i may clear that what i wanted to do is like we embed the google authenticate page to aur laravel project something like that i wanted to do but in my case the google authenticate page will my class now how and where i created that class?
@laksh Sorry I am not following. I dont see how a class and googles authentication page are connected? As far as I know, google has this page at their site. And laravel supports logging in with google if thats what you want.
Are you meaning, you need to create a Controller that you plan to use in multiple other Controllers?
Watch this entire course. I am convinced it will help you: https://laracasts.com/series/laravel-8-from-scratch
i mean to say i have more than two controllers in which same function is calling again and again so where and how i save that function or made a class to call that class only not function can you please suggest any way Thanks in advance
@laksh personally I use actions for such code https://freek.dev/1371-refactoring-to-actions
Jetstream uses these also
@Sinnbeck will they work like that as i explained?
@laksh If I understand you correctly then yes. But maybe read the article and judge for yourself
@Sinnbeck i checked it works on some functions but not on all
@laksh not sure what that means but ok
@Sinnbeck i mean to that actions will work on some functions but not on all functions
@laksh ok if you want help I suggest giving an example. But it seems that you are looking into services instead?
@Sinnbeck yess i am looking for services class and i don't know what to do for that can you please tell me?
@laksh what do you mean by a service exactly? Laravel does not have service classes but you can just add your own folder with your own classes. Do you mean service providers or the service container?
@Sinnbeck i don't know much about the service provider or service container can you tell how can i create class like service class or is there any similarity between service classes and service provider or service container
@laksh That completely depends on your definition of a "Service class". If you can explain what you mean by a service, I can tell you if they are the same (most likely not)
You can also read the docs:
@Sinnbeck so, according to me the definition of service class is like i made a class file and put functions in that class and then if i want to use that function then create object of that class and then call the function using object of that class so is there any way to made a service class like this?
@laksh Still not 100% sure what you mean. The service container can make any object if thats what you want?
$object = resolve(SomeClass::class);
$object->someMethod();
Or can you give a real world example?
@Sinnbeck i made a service class simply in app folder and its working
@Sinnbeck can you help me that how to make a constructor like suppose i made a service file and whenever i call a function from that service file constructor will generate a token and put it in session did you have any way that will help me
Thanks in advance
@laksh Make a directory app/Services and add a file like MyFirstService.php
Give it some content like
<?php
namespace App\Services;
class MyFirstService
{
public function __construct()
{
session(['foo' => 'bar']);
}
}
can you please tell about how to service file in my laravel project? Thanks in advance
@laksh the "Free"" course that @lancashireman mentioned will explain how to create classes and all about the service container so you can use dependency injection.
If new to php, I suggest taking the php basic course first, also free.
@jlrdw sorry, but i doesn't find about service container or service class anywhere in course can you please more explain it
@laksh There's a chapter in the documentation: https://laravel.com/docs/9.x/container
Do you mean, you want to create a Controller class and You will use it everywhere by extending it?
@illeas yeah and whenever i call a function from that class it firstly generate a token and then set it in the session
@laksh Maybe similar with your question https://laracasts.com/discuss/channels/laravel/adding-a-custom-class
@illeas umm no it is not similar to my question it is different can you please suggest any different answer
Please or to participate in this conversation.