Common function in Laravel?












0















I am puzzled by the following question: if there is a common code for all controllers, for example, method of concrete controller, that detects available language for model requests:



public function language(Request $request) 
{
$languages = ["en", "it"];
$default = "en";

return in_array($request->language, $languages) ? $request->language : $default;
}


So, some controllers contain this method, that I consider is amiss:



Model::where("Lang", this.language())->get();



Where to pull out this method language() avoiding duplication of code in each controller? Should it be injected service or simple static class helper?










share|improve this question




















  • 1





    I would do this by creating a helper file in your App directory, this is where I put repetitive functions and call them from there. Here is a really helpful guide: tutsforweb.com/creating-helpers-laravel

    – just_chris
    Nov 23 '18 at 21:53











  • Is it right to mess language business logic with others short functions?

    – OPV
    Nov 23 '18 at 21:54











  • I would put the language function in a helper file so you don't have to keep duplicating the code and then call it in your model, and then for the model, I would create a scope, so for example: public function scopeLang($query) { return $query->where('lang', this.language()); } Then you would call this in your controller as: Model::lang()->get();

    – just_chris
    Nov 23 '18 at 22:01













  • For each model to create the same scope?

    – OPV
    Nov 23 '18 at 22:02











  • Is it only used with one particular model or multiple models? Is it used with other aspects of your code as well or just models?

    – Ross Wilson
    Nov 23 '18 at 22:30


















0















I am puzzled by the following question: if there is a common code for all controllers, for example, method of concrete controller, that detects available language for model requests:



public function language(Request $request) 
{
$languages = ["en", "it"];
$default = "en";

return in_array($request->language, $languages) ? $request->language : $default;
}


So, some controllers contain this method, that I consider is amiss:



Model::where("Lang", this.language())->get();



Where to pull out this method language() avoiding duplication of code in each controller? Should it be injected service or simple static class helper?










share|improve this question




















  • 1





    I would do this by creating a helper file in your App directory, this is where I put repetitive functions and call them from there. Here is a really helpful guide: tutsforweb.com/creating-helpers-laravel

    – just_chris
    Nov 23 '18 at 21:53











  • Is it right to mess language business logic with others short functions?

    – OPV
    Nov 23 '18 at 21:54











  • I would put the language function in a helper file so you don't have to keep duplicating the code and then call it in your model, and then for the model, I would create a scope, so for example: public function scopeLang($query) { return $query->where('lang', this.language()); } Then you would call this in your controller as: Model::lang()->get();

    – just_chris
    Nov 23 '18 at 22:01













  • For each model to create the same scope?

    – OPV
    Nov 23 '18 at 22:02











  • Is it only used with one particular model or multiple models? Is it used with other aspects of your code as well or just models?

    – Ross Wilson
    Nov 23 '18 at 22:30
















0












0








0


1






I am puzzled by the following question: if there is a common code for all controllers, for example, method of concrete controller, that detects available language for model requests:



public function language(Request $request) 
{
$languages = ["en", "it"];
$default = "en";

return in_array($request->language, $languages) ? $request->language : $default;
}


So, some controllers contain this method, that I consider is amiss:



Model::where("Lang", this.language())->get();



Where to pull out this method language() avoiding duplication of code in each controller? Should it be injected service or simple static class helper?










share|improve this question
















I am puzzled by the following question: if there is a common code for all controllers, for example, method of concrete controller, that detects available language for model requests:



public function language(Request $request) 
{
$languages = ["en", "it"];
$default = "en";

return in_array($request->language, $languages) ? $request->language : $default;
}


So, some controllers contain this method, that I consider is amiss:



Model::where("Lang", this.language())->get();



Where to pull out this method language() avoiding duplication of code in each controller? Should it be injected service or simple static class helper?







laravel laravel-5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 7 '18 at 16:29









Cœur

18.3k9109148




18.3k9109148










asked Nov 23 '18 at 21:46









OPVOPV

1,60321338




1,60321338








  • 1





    I would do this by creating a helper file in your App directory, this is where I put repetitive functions and call them from there. Here is a really helpful guide: tutsforweb.com/creating-helpers-laravel

    – just_chris
    Nov 23 '18 at 21:53











  • Is it right to mess language business logic with others short functions?

    – OPV
    Nov 23 '18 at 21:54











  • I would put the language function in a helper file so you don't have to keep duplicating the code and then call it in your model, and then for the model, I would create a scope, so for example: public function scopeLang($query) { return $query->where('lang', this.language()); } Then you would call this in your controller as: Model::lang()->get();

    – just_chris
    Nov 23 '18 at 22:01













  • For each model to create the same scope?

    – OPV
    Nov 23 '18 at 22:02











  • Is it only used with one particular model or multiple models? Is it used with other aspects of your code as well or just models?

    – Ross Wilson
    Nov 23 '18 at 22:30
















  • 1





    I would do this by creating a helper file in your App directory, this is where I put repetitive functions and call them from there. Here is a really helpful guide: tutsforweb.com/creating-helpers-laravel

    – just_chris
    Nov 23 '18 at 21:53











  • Is it right to mess language business logic with others short functions?

    – OPV
    Nov 23 '18 at 21:54











  • I would put the language function in a helper file so you don't have to keep duplicating the code and then call it in your model, and then for the model, I would create a scope, so for example: public function scopeLang($query) { return $query->where('lang', this.language()); } Then you would call this in your controller as: Model::lang()->get();

    – just_chris
    Nov 23 '18 at 22:01













  • For each model to create the same scope?

    – OPV
    Nov 23 '18 at 22:02











  • Is it only used with one particular model or multiple models? Is it used with other aspects of your code as well or just models?

    – Ross Wilson
    Nov 23 '18 at 22:30










1




1





I would do this by creating a helper file in your App directory, this is where I put repetitive functions and call them from there. Here is a really helpful guide: tutsforweb.com/creating-helpers-laravel

– just_chris
Nov 23 '18 at 21:53





I would do this by creating a helper file in your App directory, this is where I put repetitive functions and call them from there. Here is a really helpful guide: tutsforweb.com/creating-helpers-laravel

– just_chris
Nov 23 '18 at 21:53













Is it right to mess language business logic with others short functions?

– OPV
Nov 23 '18 at 21:54





Is it right to mess language business logic with others short functions?

– OPV
Nov 23 '18 at 21:54













I would put the language function in a helper file so you don't have to keep duplicating the code and then call it in your model, and then for the model, I would create a scope, so for example: public function scopeLang($query) { return $query->where('lang', this.language()); } Then you would call this in your controller as: Model::lang()->get();

– just_chris
Nov 23 '18 at 22:01







I would put the language function in a helper file so you don't have to keep duplicating the code and then call it in your model, and then for the model, I would create a scope, so for example: public function scopeLang($query) { return $query->where('lang', this.language()); } Then you would call this in your controller as: Model::lang()->get();

– just_chris
Nov 23 '18 at 22:01















For each model to create the same scope?

– OPV
Nov 23 '18 at 22:02





For each model to create the same scope?

– OPV
Nov 23 '18 at 22:02













Is it only used with one particular model or multiple models? Is it used with other aspects of your code as well or just models?

– Ross Wilson
Nov 23 '18 at 22:30







Is it only used with one particular model or multiple models? Is it used with other aspects of your code as well or just models?

– Ross Wilson
Nov 23 '18 at 22:30














1 Answer
1






active

oldest

votes


















2














There's tons of ways to achieve what you want. Here's an example you could do. Since this function seems to be associated with the request you could add it as a macro in the request:



In one of your service providers do:



 IlluminateHttpRequest::macro('getLanguage', function () {
$languages = ["en", "it"];
$default = "en";
return in_array($this->language, $languages) ? $this->language : $default;
});


Then you can use it as part of the request e.g. :



 request()->getLanguage(); //using the helper
Request::getLanguage(); //Using the request facade
$request->getLanguage(); //If you already have a request object.





share|improve this answer

























    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
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453258%2fcommon-function-in-laravel%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    There's tons of ways to achieve what you want. Here's an example you could do. Since this function seems to be associated with the request you could add it as a macro in the request:



    In one of your service providers do:



     IlluminateHttpRequest::macro('getLanguage', function () {
    $languages = ["en", "it"];
    $default = "en";
    return in_array($this->language, $languages) ? $this->language : $default;
    });


    Then you can use it as part of the request e.g. :



     request()->getLanguage(); //using the helper
    Request::getLanguage(); //Using the request facade
    $request->getLanguage(); //If you already have a request object.





    share|improve this answer






























      2














      There's tons of ways to achieve what you want. Here's an example you could do. Since this function seems to be associated with the request you could add it as a macro in the request:



      In one of your service providers do:



       IlluminateHttpRequest::macro('getLanguage', function () {
      $languages = ["en", "it"];
      $default = "en";
      return in_array($this->language, $languages) ? $this->language : $default;
      });


      Then you can use it as part of the request e.g. :



       request()->getLanguage(); //using the helper
      Request::getLanguage(); //Using the request facade
      $request->getLanguage(); //If you already have a request object.





      share|improve this answer




























        2












        2








        2







        There's tons of ways to achieve what you want. Here's an example you could do. Since this function seems to be associated with the request you could add it as a macro in the request:



        In one of your service providers do:



         IlluminateHttpRequest::macro('getLanguage', function () {
        $languages = ["en", "it"];
        $default = "en";
        return in_array($this->language, $languages) ? $this->language : $default;
        });


        Then you can use it as part of the request e.g. :



         request()->getLanguage(); //using the helper
        Request::getLanguage(); //Using the request facade
        $request->getLanguage(); //If you already have a request object.





        share|improve this answer















        There's tons of ways to achieve what you want. Here's an example you could do. Since this function seems to be associated with the request you could add it as a macro in the request:



        In one of your service providers do:



         IlluminateHttpRequest::macro('getLanguage', function () {
        $languages = ["en", "it"];
        $default = "en";
        return in_array($this->language, $languages) ? $this->language : $default;
        });


        Then you can use it as part of the request e.g. :



         request()->getLanguage(); //using the helper
        Request::getLanguage(); //Using the request facade
        $request->getLanguage(); //If you already have a request object.






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 24 '18 at 6:17

























        answered Nov 23 '18 at 23:02









        apokryfosapokryfos

        19k43159




        19k43159
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453258%2fcommon-function-in-laravel%23new-answer', 'question_page');
            }
            );

            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







            Popular posts from this blog

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Costa Masnaga