{"id":13828514,"url":"https://github.com/spatie/laravel-view-components","last_synced_at":"2025-07-09T06:32:07.948Z","repository":{"id":57056543,"uuid":"134554591","full_name":"spatie/laravel-view-components","owner":"spatie","description":"A better way to connect data with view rendering in Laravel","archived":true,"fork":false,"pushed_at":"2020-03-24T08:46:08.000Z","size":48,"stargazers_count":238,"open_issues_count":0,"forks_count":23,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-11-06T04:19:07.533Z","etag":null,"topics":["components","html","laravel","rendering"],"latest_commit_sha":null,"homepage":"https://spatie.be/open-source/packages","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"custom":"https://spatie.be/open-source/support-us"}},"created_at":"2018-05-23T10:40:50.000Z","updated_at":"2024-09-10T21:35:35.000Z","dependencies_parsed_at":"2022-08-24T14:52:56.047Z","dependency_job_id":null,"html_url":"https://github.com/spatie/laravel-view-components","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-view-components","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-view-components/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-view-components/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-view-components/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/laravel-view-components/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224996520,"owners_count":17404486,"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":["components","html","laravel","rendering"],"created_at":"2024-08-04T09:02:50.243Z","updated_at":"2024-11-20T08:30:38.930Z","avatar_url":"https://github.com/spatie.png","language":"PHP","funding_links":["https://spatie.be/open-source/support-us"],"categories":["PHP"],"sub_categories":[],"readme":"**THIS PACKAGE HAS BEEN ABANDONED**\n\n# A better way to connect data with view rendering in Laravel\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-view-components.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-view-components)\n![GitHub Workflow Status](https://img.shields.io/github/workflow/status/spatie/laravel-view-components/run-tests?label=tests)\n[![StyleCI](https://github.styleci.io/repos/134554591/shield?branch=master)](https://github.styleci.io/repos/134554591)\n[![Quality Score](https://img.shields.io/scrutinizer/g/spatie/laravel-view-components.svg?style=flat-square)](https://scrutinizer-ci.com/g/spatie/laravel-view-components)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-view-components.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-view-components)\n\nView components are a way to help organize logic tied to view, similar to [view composers](https://laravel.com/docs/5.6/views#view-composers).\n\n```php\nnamespace App\\Http\\ViewComponents;\n\nuse Illuminate\\Http\\Request;\nuse Illuminate\\Contracts\\Support\\Htmlable;\n\nclass NavigationComponent implements Htmlable\n{\n    /** \\Illuminate\\Http\\Request */\n    private $request;\n\n    /** @var string */\n    private $backgroundColor;\n\n    public function __construct(Request $request, string $backgroundColor)\n    {\n        $this-\u003erequest = $request;\n        $this-\u003ebackgroundColor = $backgroundColor;\n    }\n\n    public function toHtml(): string\n    {\n        return view('components.navigation', [\n            'activeUrl' =\u003e $this-\u003erequest-\u003eurl(),\n            'backgroundColor' =\u003e $this-\u003ebackgroundColor,\n        ]);\n    }\n}\n```\n\n```blade\n@render('navigationComponent', ['backgroundColor' =\u003e 'black'])\n```\n\nA view component can be anything that implements Laravel's `Htmlable` contract, so you don't necessarily need to use Blade views to render the component. This is useful for wrapping third party HTML packages, like [spatie/laravel-menu](https://github.com/spatie/laravel-menu).\n\n```php\nnamespace App\\Http\\ViewComponents;\n\nuse Illuminate\\Contracts\\Support\\Htmlable;\nuse Illuminate\\Contracts\\Auth\\Guard;\nuse Spatie\\Menu\\Laravel\\Menu;\n\nclass MainMenuComponent implements Htmlable\n{\n    /** @var \\Illuminate\\Contracts\\Auth\\Guard */\n    private $guard;\n\n    /** @var string */\n    private $class;\n\n    public function __construct(Guard $guard, string $class = null)\n    {\n        $this-\u003eguard = $guard;\n        $this-\u003eclass = $class;\n    }\n\n    public function toHtml(): string\n    {\n        $menu = Menu::new()\n            -\u003eaddClass($this-\u003eclass)\n            -\u003eurl('/', 'Home')\n            -\u003eurl('/projects', 'Projects');\n\n        if ($this-\u003eguard-\u003echeck()) {\n            $menu-\u003eurl('/admin', 'Adminland');\n        }\n\n        return $menu;\n    }\n}\n```\n\n```blade\n@render('mainMenuComponent', ['class' =\u003e 'background-green'])\n```\n\nThe benefit over view composers is that data and rendering logic are explicitly tied together in components instead of being connected afterwards. They also allow you to seamlessly combine properties and dependency injection.\n\nThis package is based on *[Introducing View Components in Laravel, an alternative to View Composers](https://laravel-news.com/introducing-view-components-on-laravel-an-alternative-to-view-composers)* by [Jeff Ochoa](https://twitter.com/@Jeffer_8a).\n\n## Support us\n\nWe invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). \n\nWe highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).\n\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require spatie/laravel-view-components\n```\n\nNo additional setup necessary. Laravel will automatically discover and register the service provider.\n\nOptionally you can publish the config file with:\n\n```bash\nphp artisan vendor:publish --provider=\"Spatie\\ViewComponents\\ViewComponentsServiceProvider\" --tag=\"config\"\n```\n\nThis is the default content of the file that will be published at `config/view-components`:\n\n```php\nreturn [\n    /*\n     * The root namespace where components reside. Components can be referenced\n     * with camelCase \u0026 dot notation.\n     *\n     * Example: 'root_namespace' =\u003e App\\Http\\ViewComponents::class\n     *\n     * `@render('myComponent')\n     *     =\u003e `App\\Http\\ViewComponents\\MyComponent`\n     */\n    'root_namespace' =\u003e 'App\\Http\\ViewComponents',\n\n    /*\n     * Register alternative namespaces here, similar to custom view paths.\n     *\n     * Example: 'navigation' =\u003e App\\Services\\Navigation::class,\n     *\n     * `@render('navigation::mainMenu')`\n     *     =\u003e `App\\Services\\Navigation\\MainMenu`\n     */\n    'namespaces' =\u003e [\n        // 'navigation' =\u003e App\\Services\\Navigation::class,\n    ],\n];\n```\n\n## Usage\n\n### The `@render` directive\n\nThe `@render` Blade directive accepts two arguments: the first is the view component's path or class name, the second is an extra set of properties (optional).\n\nYou can choose between referencing the component via a path or a class name.\n\n```blade\n@render('myComponent')\n@render(App\\Http\\ViewComponents\\MyComponent::class)\n```\n\nParameters will be injected in the view components `__construct` method. The component is instantiated with Laravel's container, so parameters that aren't provided by render will be automatically injected.\n\n```php\nuse Illuminate\\Http\\Request;\n\nclass MyComponent implements Htmlable\n{\n    public function __construct(Request $request, string $color)\n    {\n        $this-\u003erequest = $request;\n        $this-\u003ecolor = $color;\n    }\n\n    // ...\n}\n```\n\n```blade\n@render('myComponent', ['color' =\u003e 'red'])\n```\n\nIn the above example, `$color` is explicitly set, and a `$request` object will be injected by Laravel.\n\n### Configuration\n\n#### The root namespace\n\nBy configuring `root_namespace`, you can define where the bulk of your view components are located. By default, this is in `App\\Http\\ViewComponents`.\n\n```\napp/\n  Http/\n    ViewComponents/\n      MyComponent.php\n      Nested/\n        NestedComponent.php\n```\n\nThe above components can be rendered with `@render('myComponent')` and `@render('nested.nestedComponent')`.\n\n#### Additional namespaces\n\nYou can register additional namespaces in the `namespaces` configuration, similar to view paths.\n\n``` php\nreturn [\n    'namespaces' =\u003e [\n        'navigation' =\u003e App\\Services\\Navigation::class,\n    ],\n];\n```\n\n```\napp/\n  Services/\n    Navigation/\n      Menu.php\n```\n\nThe above `Menu` component can now be rendered with `@render('navigation::menu')`.\n\n### Testing\n\n``` bash\ncomposer test\n```\n\n### Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n### Security\n\nIf you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.\n\n## Credits\n\n- [Sebastian De Deyne](https://github.com/sebastiandedeyne)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Flaravel-view-components","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Flaravel-view-components","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Flaravel-view-components/lists"}