Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

laksh's avatar
Level 1

Where to create class

i have to made a class which i use in any controller so where should i create that class and how

0 likes
33 replies
Sinnbeck's avatar

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

laksh's avatar
Level 1

@Sinnbeck ok thankyou one more confusion how would i call that function in controller by using $this-> or something other?

Sinnbeck's avatar

If you can show the class you created, I can better help

But an example

$foo = new Foo();
$foo->doStuff();
laksh's avatar
Level 1

@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?

Sinnbeck's avatar

@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.

https://laravel.com/docs/9.x/socialite

RayC's avatar

Are you meaning, you need to create a Controller that you plan to use in multiple other Controllers?

laksh's avatar
Level 1

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

Sinnbeck's avatar

@laksh If I understand you correctly then yes. But maybe read the article and judge for yourself

laksh's avatar
Level 1

@Sinnbeck i mean to that actions will work on some functions but not on all functions

Sinnbeck's avatar

@laksh ok if you want help I suggest giving an example. But it seems that you are looking into services instead?

laksh's avatar
Level 1

@Sinnbeck yess i am looking for services class and i don't know what to do for that can you please tell me?

Sinnbeck's avatar

@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?

laksh's avatar
Level 1

@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's avatar
Level 1

@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?

Sinnbeck's avatar

@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();
laksh's avatar
Level 1

@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

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@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']);
    }
}
laksh's avatar
Level 1

can you please tell about how to service file in my laravel project? Thanks in advance

laksh's avatar
Level 1

@jlrdw sorry, but i doesn't find about service container or service class anywhere in course can you please more explain it

illeas's avatar

Do you mean, you want to create a Controller class and You will use it everywhere by extending it?

laksh's avatar
Level 1

@illeas yeah and whenever i call a function from that class it firstly generate a token and then set it in the session

laksh's avatar
Level 1

@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.