Usage | laravel-package-tools-rust | Spatie       

 SPATIE  

laravel-package-tools-rust
==========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-package-tools-rust](https://spatie.be/docs/laravel-package-tools-rust/v1)  Usage

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-package-tools-rust/v1) 

  Usage    
- [ Introduction ](https://spatie.be/docs/laravel-package-tools-rust/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-package-tools-rust/v1/support-us)
- [ Installation ](https://spatie.be/docs/laravel-package-tools-rust/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/laravel-package-tools-rust/v1/usage)
- [ Install commands ](https://spatie.be/docs/laravel-package-tools-rust/v1/install-commands)
- [ Changelog ](https://spatie.be/docs/laravel-package-tools-rust/v1/changelog)
- [ Testing ](https://spatie.be/docs/laravel-package-tools-rust/v1/testing)

 Usage
=====

###  On this page 

1. [ Configuration and services ](#content-configuration-and-services)
2. [ Commands ](#content-commands)
3. [ Migrations ](#content-migrations)
4. [ Views and routes ](#content-views-and-routes)
5. [ Lifecycle hooks ](#content-lifecycle-hooks)

Configuration and services
--------------------------------------------------------------------------------------------------------------------------------------

Return a provider from a function. Configuration is typed, and the application can replace defaults before services register:

```
use laravel::foundation::ServiceProvider;
use spatie_package_tools::Package;

#[derive(Clone)]
struct Options { enabled: bool }

fn service_provider() -> impl ServiceProvider {
    Package::new("my-package")
        .config_defaults(Options { enabled: true })
        .register(|app| {
            app.singleton(|app| async move {
                Ok(app.config().section::<Options>().clone())
            });
        })
}

let app = laravel::Application::configure()
    .replace_env(laravel::config::Env::default())
    .with_providers(|providers| { providers.add(service_provider()); })
    .with_config(|config, _| { config.set(Options { enabled: false }); })
    .create();
assert!(!app.config().section::<Options>().enabled);
```

- Config sections passed to `.config_defaults(...)` must implement `Clone + Send + Sync + 'static`.
- Each call adds a config hook. Later values of the same type replace earlier ones.
- The application can override package sections in `with_config` and bindings in `with_services`.
- There is no configuration file to publish.

Read environment-dependent defaults in `.config(...)`:

```
use spatie_package_tools::Package;

#[derive(Clone)]
struct Options { enabled: bool }

let provider = Package::new("my-package")
    .config(|config, env| {
        config.set(Options { enabled: env.get("PACKAGE_ENABLED").unwrap_or(true) });
    });
```

Commands
--------------------------------------------------------------------------------

- Register commands by type with `.command::<YourCommand>()`.
- Define names and arguments using the native `#[console::command]` macro and clap.
- Add `clap = { version = "4", features = ["derive"] }` to the package's dependencies when defining commands.
- Repeated registrations follow the framework's last-registration-wins behavior.

```
use laravel::console::{Console, clap};
use spatie_package_tools::Package;

#[derive(clap::Parser)]
#[command(name = "package:greet")]
struct Greet { name: String }

#[laravel::console::command]
impl Greet {
    async fn handle(self, console: &mut Console) {
        console.line(format!("Hello {}", self.name));
    }
}

let provider = Package::new("my-package").command::<Greet>();
```

Migrations
--------------------------------------------------------------------------------------

- Enable the `database` feature and select a driver in your application's `laravel` dependency, or enable `sqlite` for SQLite.
- Pass `laravel::db::migrations!("migrations")` to `.migrations(...)`. The macro resolves the directory in the calling package and embeds its migrations in the binary.
- Migrations register with the application in native name order.
- Registration does not execute SQL.
- The operator runs the application's `migrate` command explicitly.
- Migration names must be unique across all packages and the application.

Views and routes
--------------------------------------------------------------------------------------------------------

- Pass `laravel::embed!("resources/views")` to `.views("my-package", ...)`.
- Render with the native name `my-package::view-name`.
- The application can replace that namespace in `with_views`.
- Directory sources are also accepted, but relative paths resolve against the application, not the package's source tree.

Use the native routing builder inside `.routes(...)`:

```
use spatie_package_tools::Package;

let provider = Package::new("status")
    .routes(|routing, _config| {
        routing.api(|route| { route.get("/package-status", || async { "ok" }); });
    });
```

- Routes see final configuration. Each routes call adds registrations.
- Authentication and middleware remain explicit native routing decisions.

Lifecycle hooks
-----------------------------------------------------------------------------------------------------

- All provider config hooks run before application configuration.
- All registration hooks run after configuration and before application registration callbacks.
- `.register(...)` accepts the native registrar. Use it for lazy bindings, events, schedules, assets or `app.discover(env!("CARGO_CRATE_NAME"))`.
- `.boot(|app| async move { ... })` receives a shared container handle after registration. An error stops startup and later boot hooks.
- Repeated hooks accumulate in declaration order within each phase, even when builder calls from different phases are interleaved.
- Resolve dependencies in service factories rather than during registration.
- Boot hooks and install steps return `laravel::Result<()>`. Package tools propagates the original error without wrapping it, so callers retain the framework's error reporting and downcasting.

See [install commands](install-commands) for explicit setup workflows.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
