Define a prefix in your controller which is automatically appended to your view- or route-names.
royvoetman/laravel-prefixer is a Laravel package for define a prefix in your controller which is automatically appended to your view- or route-names..
It currently has 2 GitHub stars and 17 downloads on Packagist (latest version v1.0.4).
Install it with composer require royvoetman/laravel-prefixer.
Discover more Laravel packages by royvoetman
or browse all Laravel packages to compare alternatives.
Last updated
This packages allows you to define a prefix in your controller which will be automatically appended to your view and/or route names.
In a resource controller, it is a common pattern to have all the associated views in the same folder, and the same goes for the location of your routes. With this feature, you can define a prefix which is automatically appended to your view-name or route-name.
composer require royvoetman/laravel-prefixer
When you want to use View prefixes your controller will have to implement the RoyVoetman\Prefixer\Contracts\ViewPrefix interface. This interface requires you to add a viewPrefix method that returns a string.
Second your Controller must include the RoyVoetman\Prefixer\Http\Traits\CreatesViews trait. This trait includes the view(string $view) method to the controller which handles the prefixing for us. The best practice is to include this trait in your BaseController . This method checks if the CreatesViews interface is implemented, if this is not the case this method will behave the same as the view() global helper function.
namespace App\Http\Controllers;
use Illuminate\Contracts\Support\Renderable;
use RoyVoetman\Prefixer\Contracts\ViewPrefix;
use RoyVoetman\Prefixer\Http\Traits\CreatesViews;
/**
* Class BookController
*
* @package App\Http\Controllers
*/
class BookController extends Controller implements ViewPrefix
{
use CreatesViews;
/**
* @return \Illuminate\Contracts\Support\Renderable
*/
public function create(): Renderable
{
// Return view: `authorized.books.create`
return $this->view('create');
}
/**
* @return \Illuminate\Contracts\Support\Renderable
*/
public function edit(Book $book): Renderable
{
// You can have chain methods like `with()` just like
// you normally would when using `return view()`
return $this->view('edit')->with('book', $book);
}
/**
* @return string
*/
public function viewPrefix(): string
{
return 'authorized.books';
}
}
Route prefixing works the same as View Prefixing except for the following:
The Controller must implement the RoyVoetman\Prefixer\Contracts\RoutePrefix interface and must include the RoyVoetman\Prefixer\Http\Traits\ForwardsRequests trait.
Instead of the viewPrefix method, you have to include a routePrefix method. And instead of the view(string $view) method you have to use the redirect(string $route) method. When the RoutePrefix method is not implemented this method will behave the same as calling redirect()->route($route).
Route prefixes only work if you are using named routes.
namespace App\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use RoyVoetman\Prefixer\Contracts\RoutePrefix;
use RoyVoetman\Prefixer\Http\Traits\ForwardsRequests;
/**
* Class BookController
*
* @package App\Http\Controllers
*/
class BookController extends Controller implements RoutePrefix
{
use ForwardsRequests;
/**
* @return \Illuminate\Http\RedirectResponse
*/
public function store(): RedirectResponse
{
...
// Redirect to: `books.index`
return $this->redirect('index');
}
/**
* @return \Illuminate\Http\RedirectResponse
*/
public function update(Book $book): RedirectResponse
{
...
// You can have chain methods like `with()` just like
// you normally would when using `return redirect()`
return $this->redirect('index')->with('status', 'Book updated');
}
/**
* @return string
*/
public function routePrefix(): string
{
return 'books';
}
}
There is a convenient shortcut when you want to implement the ViewPrefix and the RoutePrefix interface. You can include the RoyVoetman\Prefixer\Contracts\ResponsePrefixes interface which just extends both interfaces.
/**
* Interface ResponsePrefixes
*
* @package App\Interfaces
*/
interface ResponsePrefixes extends RoutePrefix, ViewPrefix
{
//
}
Please see CHANGELOG for more information what has changed recently.
Contributions are welcome and will be fully credited. We accept contributions via Pull Requests on Github.
README.md and any other relevant documentation are kept up-to-date.The MIT License (MIT). Please see License File for more information.