Where do I put my custom classes and functions in Laravel
I'm trying to build a class with some frequently used functions that I can use from anywhere in my project. I don't know where to build the PHP file with the classes in it or how to recall them... Can anyone help me figure out where all this stuff fits in? THANKS!!!
/App/Http/Helpers/MyClasses.php
<?php
class PopularFunctions {
public function sayhi() {
echo 'hi';
}
}
?>
/App/Http/Controllers/TasksController.php
<?php
namespace AppHttpControllers;
use AppHttpHelpersMyClasses;
class TasksController extends Controller {
public function index() {
$myfunctions = new PopularFunctions();
$myfunctions->sayhi();
}
}
This returns: Class 'AppHttpControllersPopularFunctions' not found.
php laravel function class namespaces
add a comment |
I'm trying to build a class with some frequently used functions that I can use from anywhere in my project. I don't know where to build the PHP file with the classes in it or how to recall them... Can anyone help me figure out where all this stuff fits in? THANKS!!!
/App/Http/Helpers/MyClasses.php
<?php
class PopularFunctions {
public function sayhi() {
echo 'hi';
}
}
?>
/App/Http/Controllers/TasksController.php
<?php
namespace AppHttpControllers;
use AppHttpHelpersMyClasses;
class TasksController extends Controller {
public function index() {
$myfunctions = new PopularFunctions();
$myfunctions->sayhi();
}
}
This returns: Class 'AppHttpControllersPopularFunctions' not found.
php laravel function class namespaces
Please add your folder structure also the path and code of the class you mentioned.
– HCK
Nov 22 at 19:06
2
You should be able to put a new class virtually anywhere in theapp
directory. There's no real restrictions. If you post your code and structure etc. we might be able to help you pinpoint the problem.
– Jonathon
Nov 22 at 19:11
Laravel comes configured to use the composer PSR-4 autoloader and the root namespace isApp
and is under theapp
folder so as long as you follow the standard and place your files underapp
they should be picked up. If not trycomposer dump-autoload
to re-create the autoload file
– apokryfos
Nov 22 at 20:30
Thanks. I updated my question. Hope this clarifies.
– Topher
Nov 25 at 2:50
add a comment |
I'm trying to build a class with some frequently used functions that I can use from anywhere in my project. I don't know where to build the PHP file with the classes in it or how to recall them... Can anyone help me figure out where all this stuff fits in? THANKS!!!
/App/Http/Helpers/MyClasses.php
<?php
class PopularFunctions {
public function sayhi() {
echo 'hi';
}
}
?>
/App/Http/Controllers/TasksController.php
<?php
namespace AppHttpControllers;
use AppHttpHelpersMyClasses;
class TasksController extends Controller {
public function index() {
$myfunctions = new PopularFunctions();
$myfunctions->sayhi();
}
}
This returns: Class 'AppHttpControllersPopularFunctions' not found.
php laravel function class namespaces
I'm trying to build a class with some frequently used functions that I can use from anywhere in my project. I don't know where to build the PHP file with the classes in it or how to recall them... Can anyone help me figure out where all this stuff fits in? THANKS!!!
/App/Http/Helpers/MyClasses.php
<?php
class PopularFunctions {
public function sayhi() {
echo 'hi';
}
}
?>
/App/Http/Controllers/TasksController.php
<?php
namespace AppHttpControllers;
use AppHttpHelpersMyClasses;
class TasksController extends Controller {
public function index() {
$myfunctions = new PopularFunctions();
$myfunctions->sayhi();
}
}
This returns: Class 'AppHttpControllersPopularFunctions' not found.
php laravel function class namespaces
php laravel function class namespaces
edited Nov 24 at 2:55
asked Nov 22 at 19:04
Topher
54
54
Please add your folder structure also the path and code of the class you mentioned.
– HCK
Nov 22 at 19:06
2
You should be able to put a new class virtually anywhere in theapp
directory. There's no real restrictions. If you post your code and structure etc. we might be able to help you pinpoint the problem.
– Jonathon
Nov 22 at 19:11
Laravel comes configured to use the composer PSR-4 autoloader and the root namespace isApp
and is under theapp
folder so as long as you follow the standard and place your files underapp
they should be picked up. If not trycomposer dump-autoload
to re-create the autoload file
– apokryfos
Nov 22 at 20:30
Thanks. I updated my question. Hope this clarifies.
– Topher
Nov 25 at 2:50
add a comment |
Please add your folder structure also the path and code of the class you mentioned.
– HCK
Nov 22 at 19:06
2
You should be able to put a new class virtually anywhere in theapp
directory. There's no real restrictions. If you post your code and structure etc. we might be able to help you pinpoint the problem.
– Jonathon
Nov 22 at 19:11
Laravel comes configured to use the composer PSR-4 autoloader and the root namespace isApp
and is under theapp
folder so as long as you follow the standard and place your files underapp
they should be picked up. If not trycomposer dump-autoload
to re-create the autoload file
– apokryfos
Nov 22 at 20:30
Thanks. I updated my question. Hope this clarifies.
– Topher
Nov 25 at 2:50
Please add your folder structure also the path and code of the class you mentioned.
– HCK
Nov 22 at 19:06
Please add your folder structure also the path and code of the class you mentioned.
– HCK
Nov 22 at 19:06
2
2
You should be able to put a new class virtually anywhere in the
app
directory. There's no real restrictions. If you post your code and structure etc. we might be able to help you pinpoint the problem.– Jonathon
Nov 22 at 19:11
You should be able to put a new class virtually anywhere in the
app
directory. There's no real restrictions. If you post your code and structure etc. we might be able to help you pinpoint the problem.– Jonathon
Nov 22 at 19:11
Laravel comes configured to use the composer PSR-4 autoloader and the root namespace is
App
and is under the app
folder so as long as you follow the standard and place your files under app
they should be picked up. If not try composer dump-autoload
to re-create the autoload file– apokryfos
Nov 22 at 20:30
Laravel comes configured to use the composer PSR-4 autoloader and the root namespace is
App
and is under the app
folder so as long as you follow the standard and place your files under app
they should be picked up. If not try composer dump-autoload
to re-create the autoload file– apokryfos
Nov 22 at 20:30
Thanks. I updated my question. Hope this clarifies.
– Topher
Nov 25 at 2:50
Thanks. I updated my question. Hope this clarifies.
– Topher
Nov 25 at 2:50
add a comment |
3 Answers
3
active
oldest
votes
In Laravel Framework you can only create a controller inside the appHttpController folder. If you want to create a custom class then created inside app folder.
Example:
File: appFAReports.php
namespace App;
Class FAReports {
// DEF //
}
add a comment |
create a directory say "Helpers" inside App/Http
create one class inside Helpers directory CustomAvatar.php
<?php
class CustomAvatar{
public $default_avatar='avatar.png';
public function make_custom_avatar(){
// do your operation here
}
}
?>
now if you want to use this class inside your controller :
use AppHttpHelpersCustomAvatar;
...
public function create_user(){
$customAvatar=new CustomAvatar();
$defaultAvatar = $customAvatar->default_avatar;
$user=new User();
$user->avatar=$defaultAvatar;
$user->save();
}
add a comment |
1. Through Composer
App/Http/Helpers/MyClasses.php
<?php
function sayhi() {
echo 'hi';
}
?>
then in composer.json in "autoload": { }
add
"files": [
"app/Http/Helpers/MyClasses.php"
]
so the structure will be
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\": "app/"
},
"files": [
"app/Http/Helpers/MyClasses.php"
]
},
after you change the autoload. Then run composer dump-autoload
then in controller just call the function in your helpers
public function index() {
$res = say_hi();
}
2. Class
App/Http/Helpers/MyClasses.php
<?php
namespace AppHttpHelpers;
class MyClassess {
function sayhi() {
echo 'hi';
}
}
?>
In your controller
<?php
namespace AppHttpControllers;
use AppHttpHelpersMyClasses;
class TasksController extends Controller {
public function index() {
$myfunctions = new MyClasses();
$res = $myfunctions->sayhi();
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436841%2fwhere-do-i-put-my-custom-classes-and-functions-in-laravel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
In Laravel Framework you can only create a controller inside the appHttpController folder. If you want to create a custom class then created inside app folder.
Example:
File: appFAReports.php
namespace App;
Class FAReports {
// DEF //
}
add a comment |
In Laravel Framework you can only create a controller inside the appHttpController folder. If you want to create a custom class then created inside app folder.
Example:
File: appFAReports.php
namespace App;
Class FAReports {
// DEF //
}
add a comment |
In Laravel Framework you can only create a controller inside the appHttpController folder. If you want to create a custom class then created inside app folder.
Example:
File: appFAReports.php
namespace App;
Class FAReports {
// DEF //
}
In Laravel Framework you can only create a controller inside the appHttpController folder. If you want to create a custom class then created inside app folder.
Example:
File: appFAReports.php
namespace App;
Class FAReports {
// DEF //
}
answered Nov 22 at 19:09
Arjun Jain
87616
87616
add a comment |
add a comment |
create a directory say "Helpers" inside App/Http
create one class inside Helpers directory CustomAvatar.php
<?php
class CustomAvatar{
public $default_avatar='avatar.png';
public function make_custom_avatar(){
// do your operation here
}
}
?>
now if you want to use this class inside your controller :
use AppHttpHelpersCustomAvatar;
...
public function create_user(){
$customAvatar=new CustomAvatar();
$defaultAvatar = $customAvatar->default_avatar;
$user=new User();
$user->avatar=$defaultAvatar;
$user->save();
}
add a comment |
create a directory say "Helpers" inside App/Http
create one class inside Helpers directory CustomAvatar.php
<?php
class CustomAvatar{
public $default_avatar='avatar.png';
public function make_custom_avatar(){
// do your operation here
}
}
?>
now if you want to use this class inside your controller :
use AppHttpHelpersCustomAvatar;
...
public function create_user(){
$customAvatar=new CustomAvatar();
$defaultAvatar = $customAvatar->default_avatar;
$user=new User();
$user->avatar=$defaultAvatar;
$user->save();
}
add a comment |
create a directory say "Helpers" inside App/Http
create one class inside Helpers directory CustomAvatar.php
<?php
class CustomAvatar{
public $default_avatar='avatar.png';
public function make_custom_avatar(){
// do your operation here
}
}
?>
now if you want to use this class inside your controller :
use AppHttpHelpersCustomAvatar;
...
public function create_user(){
$customAvatar=new CustomAvatar();
$defaultAvatar = $customAvatar->default_avatar;
$user=new User();
$user->avatar=$defaultAvatar;
$user->save();
}
create a directory say "Helpers" inside App/Http
create one class inside Helpers directory CustomAvatar.php
<?php
class CustomAvatar{
public $default_avatar='avatar.png';
public function make_custom_avatar(){
// do your operation here
}
}
?>
now if you want to use this class inside your controller :
use AppHttpHelpersCustomAvatar;
...
public function create_user(){
$customAvatar=new CustomAvatar();
$defaultAvatar = $customAvatar->default_avatar;
$user=new User();
$user->avatar=$defaultAvatar;
$user->save();
}
answered Nov 22 at 19:14
Saurabh Mistry
3,1971726
3,1971726
add a comment |
add a comment |
1. Through Composer
App/Http/Helpers/MyClasses.php
<?php
function sayhi() {
echo 'hi';
}
?>
then in composer.json in "autoload": { }
add
"files": [
"app/Http/Helpers/MyClasses.php"
]
so the structure will be
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\": "app/"
},
"files": [
"app/Http/Helpers/MyClasses.php"
]
},
after you change the autoload. Then run composer dump-autoload
then in controller just call the function in your helpers
public function index() {
$res = say_hi();
}
2. Class
App/Http/Helpers/MyClasses.php
<?php
namespace AppHttpHelpers;
class MyClassess {
function sayhi() {
echo 'hi';
}
}
?>
In your controller
<?php
namespace AppHttpControllers;
use AppHttpHelpersMyClasses;
class TasksController extends Controller {
public function index() {
$myfunctions = new MyClasses();
$res = $myfunctions->sayhi();
}
}
add a comment |
1. Through Composer
App/Http/Helpers/MyClasses.php
<?php
function sayhi() {
echo 'hi';
}
?>
then in composer.json in "autoload": { }
add
"files": [
"app/Http/Helpers/MyClasses.php"
]
so the structure will be
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\": "app/"
},
"files": [
"app/Http/Helpers/MyClasses.php"
]
},
after you change the autoload. Then run composer dump-autoload
then in controller just call the function in your helpers
public function index() {
$res = say_hi();
}
2. Class
App/Http/Helpers/MyClasses.php
<?php
namespace AppHttpHelpers;
class MyClassess {
function sayhi() {
echo 'hi';
}
}
?>
In your controller
<?php
namespace AppHttpControllers;
use AppHttpHelpersMyClasses;
class TasksController extends Controller {
public function index() {
$myfunctions = new MyClasses();
$res = $myfunctions->sayhi();
}
}
add a comment |
1. Through Composer
App/Http/Helpers/MyClasses.php
<?php
function sayhi() {
echo 'hi';
}
?>
then in composer.json in "autoload": { }
add
"files": [
"app/Http/Helpers/MyClasses.php"
]
so the structure will be
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\": "app/"
},
"files": [
"app/Http/Helpers/MyClasses.php"
]
},
after you change the autoload. Then run composer dump-autoload
then in controller just call the function in your helpers
public function index() {
$res = say_hi();
}
2. Class
App/Http/Helpers/MyClasses.php
<?php
namespace AppHttpHelpers;
class MyClassess {
function sayhi() {
echo 'hi';
}
}
?>
In your controller
<?php
namespace AppHttpControllers;
use AppHttpHelpersMyClasses;
class TasksController extends Controller {
public function index() {
$myfunctions = new MyClasses();
$res = $myfunctions->sayhi();
}
}
1. Through Composer
App/Http/Helpers/MyClasses.php
<?php
function sayhi() {
echo 'hi';
}
?>
then in composer.json in "autoload": { }
add
"files": [
"app/Http/Helpers/MyClasses.php"
]
so the structure will be
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\": "app/"
},
"files": [
"app/Http/Helpers/MyClasses.php"
]
},
after you change the autoload. Then run composer dump-autoload
then in controller just call the function in your helpers
public function index() {
$res = say_hi();
}
2. Class
App/Http/Helpers/MyClasses.php
<?php
namespace AppHttpHelpers;
class MyClassess {
function sayhi() {
echo 'hi';
}
}
?>
In your controller
<?php
namespace AppHttpControllers;
use AppHttpHelpersMyClasses;
class TasksController extends Controller {
public function index() {
$myfunctions = new MyClasses();
$res = $myfunctions->sayhi();
}
}
edited Nov 24 at 3:25
answered Nov 24 at 3:18
Kelvin
362224
362224
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436841%2fwhere-do-i-put-my-custom-classes-and-functions-in-laravel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please add your folder structure also the path and code of the class you mentioned.
– HCK
Nov 22 at 19:06
2
You should be able to put a new class virtually anywhere in the
app
directory. There's no real restrictions. If you post your code and structure etc. we might be able to help you pinpoint the problem.– Jonathon
Nov 22 at 19:11
Laravel comes configured to use the composer PSR-4 autoloader and the root namespace is
App
and is under theapp
folder so as long as you follow the standard and place your files underapp
they should be picked up. If not trycomposer dump-autoload
to re-create the autoload file– apokryfos
Nov 22 at 20:30
Thanks. I updated my question. Hope this clarifies.
– Topher
Nov 25 at 2:50