{"id":19450892,"url":"https://github.com/morning-train/laravelcontext","last_synced_at":"2025-04-25T03:32:10.424Z","repository":{"id":57019844,"uuid":"176496545","full_name":"Morning-Train/LaravelContext","owner":"Morning-Train","description":null,"archived":false,"fork":false,"pushed_at":"2023-06-28T09:50:28.000Z","size":95,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-03T15:44:39.975Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Morning-Train.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-19T11:24:28.000Z","updated_at":"2023-01-24T11:27:50.000Z","dependencies_parsed_at":"2024-11-10T16:40:50.740Z","dependency_job_id":"d038ed03-3de6-49cd-ac31-e5bd3d20e076","html_url":"https://github.com/Morning-Train/LaravelContext","commit_stats":{"total_commits":52,"total_committers":4,"mean_commits":13.0,"dds":"0.11538461538461542","last_synced_commit":"ee991c635efeccde8d67b8800436d2fd73f9882b"},"previous_names":[],"tags_count":39,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morning-Train%2FLaravelContext","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morning-Train%2FLaravelContext/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morning-Train%2FLaravelContext/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morning-Train%2FLaravelContext/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Morning-Train","download_url":"https://codeload.github.com/Morning-Train/LaravelContext/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250748108,"owners_count":21480779,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T16:39:22.628Z","updated_at":"2025-04-25T03:32:10.103Z","avatar_url":"https://github.com/Morning-Train.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Context\n\n\n## Installation\nThe Laravel Context package can be installed from packagist using the follow command\n\n`composer require morningtrain/laravel-context`\n\n### Facade setup\nThere is a facade helper for accessing the current context. \nAdd it as an alias to **config/app.php** using the following snippet.\n\n`'Context' =\u003e MorningTrain\\Laravel\\Context\\Context::class,`\n\n### Service provider\nContexts are configured in a service provider. Add a service provider in **app/Providers** that extends *MorningTrain\\Laravel\\Context\\ContextServiceProvider*\n\nThis could be a typical starter ContextServiceProvider which also should be registered in the **config/app.php** config file in Laravel.\n\n```php\n\u003c?php\n\nnamespace App\\Providers;\n\nuse App\\Context\\BaseContext;\nuse App\\Context\\App\\AppContext;\nuse MorningTrain\\Laravel\\Context\\ContextServiceProvider as ServiceProvider;\nuse MorningTrain\\Laravel\\Context\\Plugins\\Assets\\AssetsPlugin;\nuse MorningTrain\\Laravel\\Context\\Plugins\\Env\\EnvPlugin;\nuse MorningTrain\\Laravel\\Context\\Plugins\\Menus\\MenusPlugin;\nuse MorningTrain\\Laravel\\Context\\Plugins\\Meta\\MetaPlugin;\nuse MorningTrain\\Laravel\\Context\\Plugins\\Routes\\RoutesPlugin;\n\nclass ContextServiceProvider extends ServiceProvider\n{\n\n    /**\n     * Plugins to load\n     *\n     * @var array\n     */\n    protected $plugins = [\n        AssetsPlugin::class,\n        EnvPlugin::class,\n        MenusPlugin::class,\n        RoutesPlugin::class,\n        MetaPlugin::class\n    ];\n\n    /**\n     * Features to define\n     *\n     * @var array\n     */\n    protected $contexts = [\n        'base'  =\u003e BaseContext::class,\n        'app'   =\u003e AppContext::class,\n    ];\n\n    /**\n     * Features to load\n     *\n     * @var array\n     */\n    protected $load = [\n        'base'\n    ];\n\n}\n```\n\nIt contains 3 properties that should be configured. \n\nPlugins are extra functionality that can be added which extends the normal behaviour of the context system.\n\nThe plugins displayed above are the typically used plugins - they are all shipped with the context package. \nIs is possible to develop project-specific plugins and hook them up here.\n\nThe *contexts* property defines and array of all contexts available in the system. They will be referenced by the configured name.\nThere will be a definition of the context classsed later.\n\n*load* defines the contexts that should be automatically loaded. Contexts need to be defined here or loaded manually in order to provide any functionality.\n\n### Context class\nThis is an example of a basic AppContext class.\n\n\n```php\n\u003c?php\n\nnamespace App\\Context\\App;\n\nuse MorningTrain\\Laravel\\Context\\Context;\n\nclass AppContext\n{\n\n    public function load()\n    {\n        // Provide the app name to ENV\n        Context::env(function () {\n            return [\n                'app' =\u003e [\n                    'name' =\u003e config('app.name')\n                ]\n            ];\n        });\n\n        // Load assets\n        Context::load(Assets::class);\n\n    }\n\n}\n\n```\n\nAnything in the array returned by the closure added to the *env* method will be merged into the *env* variable for that context.\nIt can be used to provide a set of variables to for instance JavaScript.\n\n\nNote the last call to `Context::load(Asset::class)`.\nThe *Asset* class is a simple class containing a load method. \nIt is not essentially needed but is a way to split the context loading into multiple smaller and more maintainable classes.\n\nThis is a basic example of an *Asset* class.\n\n```php\n\u003c?php\n\nnamespace App\\Context\\App;\n\nuse MorningTrain\\Laravel\\Context\\Context;\n\nclass Assets\n{\n\n    protected $manifest = '';\n\n    public function load()\n    {\n        Context::stylesheets([\n            asset(mix('css/app.css', $this-\u003emanifest))\n        ]);\n\n        Context::scripts([\n            asset(mix('js/manifest.js', $this-\u003emanifest)),\n            asset(mix('js/vendor.js', $this-\u003emanifest)),\n            asset(mix('js/app.js', $this-\u003emanifest))\n        ]);\n    }\n\n}\n```\n\nThis is where stylesheets and JavaScripts are configured and made available to the HTML blade view. \nThis allows for more dynamic loading of scripts and keeps the base HTML templates clean.\n\n### Context provider\nContextProviders were added in version 2.5.0 and is an updated approach to structuring the context class.\n\nNew features include the ability to register plugins directly from any ContextProvider, meaning that plugins can be removed from the ContextServiceProvider.\nThis shift away from registering the plugins in the service provider will leave plugin dependency to the classes actually implementing them.\n\nThe *Asset* example above and similar classes are now called *partials* and can be automatically loaded from any ContextProvider.\n\nIt is recommended that these partials also extends the `ContextProvider` base class to have support for plugins.   \n\nNote that it is encouraged to split the context class up into partials instead of having custom logic inside the context class itself. \n\nThe `AppContext` example will the look like this (without the app name example).\n\n```php\n\u003c?php\n\nnamespace App\\Context\\App;\n\nuse MorningTrain\\Laravel\\Context\\ContextProvider;\n\nclass AppContext extends ContextProvider\n{\n\n    protected $partials = [\n        Assets::class,\n    ];\n\n}\n\n```\n\nAnd the Assets class like this\n\n```php\n\u003c?php\n\nnamespace App\\Context\\App;\n\nuse MorningTrain\\Laravel\\Context\\Context;\nuse MorningTrain\\Laravel\\Context\\ContextProvider;\nuse MorningTrain\\Laravel\\Context\\Plugins\\Assets\\AssetsPlugin;\n\nclass Assets extends ContextProvider\n{\n\n    public static $plugins = [\n        AssetsPlugin::class\n    ];\n    \n    protected $manifest = '';\n\n    public function load()\n    {\n        Context::stylesheets([\n            asset(mix('css/app.css', $this-\u003emanifest))\n        ]);\n\n        Context::scripts([\n            asset(mix('js/manifest.js', $this-\u003emanifest)),\n            asset(mix('js/vendor.js', $this-\u003emanifest)),\n            asset(mix('js/app.js', $this-\u003emanifest))\n        ]);\n    }\n\n}\n\n```\n\n### Middleware\nRemember to add the context middleware to *app\\Http\\Kernel.php*.\n\nThe following snippet should be added as a route middleware.\n\n`'context' =\u003e \\MorningTrain\\Laravel\\Context\\Middleware\\LoadFeatures::class,`\n\nTo work with ContextProviders, the updated version should be used instead:\n\n`'context' =\u003e \\MorningTrain\\Laravel\\Context\\Middleware\\LoadContexts::class,`\n\nTo keep backward compatibility, the old middleware is kept for now.\n\n## Loading contexts\nBeside from loading contexts in the service provider, one would use the context middleware to load a context.\n\nAdd this middleware to any route group to load the **app** context during the load of those routes.\n\n`'context:app'`\n\nThis way, any route under the group will gain access to any information added to the app context.\n\n## Including scripts and stylesheets\nAdd `{!! Context::stylesheets() !!}` to the document header in order to generate HTML tags for all enqueued stylesheets.\n\nAdd `{!! Context::scripts() !!}` to the HTML body footer in order to generate script tags for all enqueued JavaScript files.\n\n## Providing ENV to JavaScript\nUse the following script in your blade HTML template in order to automatically generate a JavaScript object class *env*.\n\nIt will be an object representing the entire environment built in the loaded context.\n\n`{!! Context::env() !!}`\n\n# Credits\nThis package is developed and actively maintained by [Morningtrain](https://morningtrain.dk).\n\n\u003c!-- language: lang-none --\u003e\n     _- _ -__ - -- _ _ - --- __ ----- _ --_  \n    (         Morningtrain, Denmark         )\n     `---__- --__ _ --- _ -- ___ - - _ --_ ´ \n         o                                   \n        .  ____                              \n      _||__|  |  ______   ______   ______ \n     (        | |      | |      | |      |\n     /-()---() ~ ()--() ~ ()--() ~ ()--() \n    --------------------------------------\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorning-train%2Flaravelcontext","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmorning-train%2Flaravelcontext","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorning-train%2Flaravelcontext/lists"}