I'm trying to link in my package to a view also in the same package.
This is the file structure:
/report/src
/report/src/ReportServiceProvider.php
/report/src/views/test.blade.php
/report/src/SomeClass.php
In my ReportServiceProvider.php I specify the directory where the views should be loading from (like specified here):
public function boot()
{
$this->loadViewsFrom(__DIR__.'/views', 'reports');
}
With the 'hint' reports, so I should be able to access them with view('reports::test')
Off course I add my ServiceProvider to /config/app.php's providers array like so:
....
Vendor\Report\ReportServiceProvider::class,
....
I load my package in composer as follows:
"autoload": {
....
"psr-4": {
"App\\": "app/",
"Vendor\\Report\\": "packages/vendor/report/src"
}
...
}
But when I use the view('reports::test') in SomeClass.php i get the following error:
No hint path defined for [reports]
So somehow it cannot find the reports hint....
What am I missing here?
I had a similar issue for Laravel's default forgot-password functionality. Error No hint path defined for [emails]
Running php artisan optimize:clear fixed it for me.
I think report is your package name,
Step 1: You must specify the package name inside the service provider
$this->loadViewsFrom(__DIR__.'/views', 'report');
Step 2: If you want to load the view
return view('packageName::Email.testmail'); //packageName is report, the actual path to my view is package/report/src/views/Email/testmail.blade.php
You need to add the package' service provider in cofing/app.php
I solved the error No hint path defined for [view] by putting the following code snippet on my service provider boot method of my package:
$this->loadViewsFrom(__DIR__.'/views', 'home');
Where home is my view file home.blade.php. As I am Laravel beginner, maybe in package builded type of coding in need to give the path of view files inside service provider.
if you have your blades views in ...views/xxx, that is how you can specify it:
app('xxx')->addNamespace('mail', resource_path('views') . '/xxx');
If you're using infyomlabs/adminlte-templates package just add service provider in config/app.php as shown below:
'providers' => [
// ...
InfyOm\AdminLTETemplates\AdminLTETemplatesServiceProvider::class,
// ...
],
#include('cookie-consent::index') this should be used the blade file
either in the body tag or footer tag
<body>
#include('cookie-consent::index')
</body>
<footer>
#include('cookie-consent::index')
</footer>
#include('cookieConsent::index') /**this might not work */
Related
I'm on the way of creating a Laravel package for our internal use, that's why it's not in packagist, and not in vendor/ directory. Instead it's in packages/ directory - a custom directory of ours - a similar approach we used for another package of our internal use.
Issue is, in the blades from the package we used action('MyPackageController#method') to produce links. After publishing the view files to the application the current path of the views are like resources/views/vendor/mypackage/one-directory/index.blade.php.
With a global access to the controller it simply is working:
action('\MyVendor\MyPackage\App\Controllers\MyPackageController#edit', ['id' => $item->id])
But issue is, when I'm trying to impose aliases for the controller[s], they are not working. In config/app.php aliases, I mentioned:
'XYZ' => MyVendor\MyPackage\App\Controllers\MyPackageController::class,
'ABC' => MyVendor\MyPackage\App\Controllers\MyPackageSecondController::class,
and in index.blade.php I tried using:
action('ABC#edit', ['id' => $item->id])
but it's not working. Because it's trying to find the controller in App\Http\Controllers\:
ErrorException (E_ERROR)
Action App\Http\Controllers\ABC#edit not defined. (View: D:\laragon\www\test-laravel\resources\views\vendor\mypackage\one-directory\index.blade.php)
Aren't package controllers be aliased?
PS. I tried clearing all types of caching.
As even after a long time, Martin Bean did not add any answer to the thread, here I'm posting one for future record:
As Martin Bean said in his comment:
You’d used a named route instead of action(): laravel.com/docs/master/routing#named-routes
What I found is: the action() function has its own caveat, it cannot take other namespaces other than App\Http\Controllers. So named route did it for me: route('my_name', ['id' => $item->id]);.
Thanks to Martin Bean for his clue.
First of all, you have to register your routes in the service provider of your package.
boot() {
require __DIR__ . '/Http/routes.php';
}
__DIR__ would point to the root of your package folder. This is assuming your routes are registered in your packages' Http directory. If this is done, make sure you use a unique name for your route and just call your action like so:
action="{{route-name}}"
That's all you have to do.
For my Laravel 5.5 project I used filemanager package (elfinder-laravel ) with published and adapted blade views. After awhile I found that default views are used from package folder:
/vendor/barryvdh/laravel-elfinder/resources/views
instead of published views:
/resources/views/vendor/elfinder
I tried to republish views, clear views and cache. But nothing helps, it still uses default package views.
Views in /resources/views/vendor/elfinder exist.
Any idea how to make it work?
Try to edit the service provider file to be like that :
public function boot()
{
$this->loadViewsFrom($this->app->resourcePath('views/vendor/elfinder'), 'elfinder');
}
It is forces the file to use this directory as main view directory instead of changing it.
The question is quite old, but you have to set in the
$this->loadViewsFrom(__DIR__.'/../resources/views', 'courier');
the same package name as in the
$this->publishes([
__DIR__.'/../resources/views' => resource_path('views/vendor/courier'),
]);
(notice both times the courier)
See: https://laravel.com/docs/8.x/packages#overriding-package-views
I have created a package in Laravel 5.4 that sets up a basic backoffice. This package contains several routes that are using controllers from within the package. What I want to be able to do is to override the package defined routes in my application in order to plug custom controllers. For example, if I have a route
Route::get('login', [
'as' => 'admin.login',
'uses' => 'Auth\LoginController#showLoginForm'
]);
defined in my package that will use the Vendor\Package\Controllers\Auth\LoginController I want to defined a route for my application that will override that one and use the App\Controllers\Auth\LoginController.
Doing the obvious approach of defining the route in app routes files fails for the app routes files are run before the package routes file, so the package definition will prevail.
Is there any way to accomplish something of this kind?
I also tried to get the particular route in the RouteServiceProvider and use the method uses to set the controller that should be used to resolve it, like this
public function boot()
{
parent::boot();
Route::get('admin.login')->uses('App\Http\Controllers\Admin\Auth\LoginController#showLoginForm');
}
but this also fails to accomplish what is pretended.
Any clues on what I am doing wrong?
In config/app.php in the providers array put the service provider of the package before App\Providers\RouteServiceProvider::class, and then in your web.php routes you'll be able to override it with your custom route.
EDIT
For Laravel package auto discovery you can disable the package being auto discovered in your composer.json like this:
"extra": {
"laravel": {
"dont-discover": [
"barryvdh/laravel-debugbar"
]
}
},
In this example the barryvdh/laravel-debugbar package won't be autodiscovered, which means you would have to manually include its service provider in the config array and then you'll be able to rearrange your custom provider in the desired order.
Another option -- which doesn't have to muck with the order of service providers -- is to add a binding for the controller.
So e.g. in AppServiceProvider,
$this->app->bind(
\Vendor\Package\Controllers\Auth\LoginController::class,
App\Controllers\Auth\LoginController::class
);
You'll have to match controller method names, but you're doing that already in your example.
(Caveat on this answer: I haven't tested it in Laravel 5.4, but I just did this in Laravel 6.0 using the $bindings property which was added in Laravel 5.6. That said, this should be correct 5.4 syntax for doing the same thing).
Edit: For Laravel 6+ you can instead add the binding to the bindings array in AppServiceProvider:
public $bindings = [
\Vendor\Package\Controllers\Auth\LoginController::class =>
App\Controllers\Auth\LoginController::class,
// other bindings
]
I have some php lines which will be used very often in my application. So I would like to build a function and call it every where in my app. My function could be :
static function array_matiere($id_ecole) {
return $something;
}
I tried to put this code in a controller and then call it like that :
$liste_matieres = MatieresController::array_matieres(Session::get('id_ecole'));
It works, but where to put this kind of function ? in the controller ? in a method file ? what is the best practice ?
Thanks for your responses.
Dominique
There are multiple ways to implement this. look here
Method 1 This method is highly suggested by Laravel Expert ( JeffreyWay)
If your helper functions are similar to the way laravel 5 ones work, i.e Illuminate/Support/helpers.php you could just create a helpers.php and have composer do the autoloading for you.
Edit your composer.json file and add the file to the autoloading key. In my example my app is called Tasky so just add the files key and reference helpers.php.
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"Tasky\\": "app/"
},
"files": [
"app/Support/helpers.php"
]
},
That way you just create your file inside app/Support/helpers.php and all your common function based view helpers or other helpers can go here. If you take the class based approach then the above is great.
Also then remember to just run the following once.
$ composer dumpautoload
Source
Method 2
Please Follow the link and read answer & Its comments
Whenever u need to include a piece of code providing some functionality through out your application then the best way in Laravel is to make Service class inside app/Services and then you can use that class anywhere in your application using
use App\Services\YourServiceName;
I have a project that uses the Sentinel bundle for Laravel.
A while ago, I asked a question about extending a model provided in the bundle. The answer I accepted worked, but it required editing the bundle's code in the vendor folder.
Since then, I've run a composer update command and the change I applied was overwritten (no surprise there).
I know a bit more about how laravel works now so I was able to trace back the end point where the bundle's services are referenced by my app. In my config/app.php file I have the service provider reference:
'Sentinel\SentinelServiceProvider',
The SentinelServiceProvider service has a register method that registers the RepoServiceProvider which is the class I need to override.
My plan is to create two new files: ExtendedSentinelServiceProvider.php and ExtendedRepoServiceProvider.php.
In the ExtendedRepoServiceProvider.php file, I could replace the class being used for the User Repository binding with my custom User class:
// Bind the User Repository
$app->bind('Sentinel\Repo\User\UserInterface', function($app)
{
return new ExtendedSentryUser( // This is my custom model that I want the ExtendedRepoServiceProvider to use
$app['sentry']
);
});
In the ExtendedSentinelServiceProvider.php file, I would replace the existing RepoServiceProvider class reference with my newly modified ExtendedRepoServiceProvider class:
public function register()
{
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Sentry', 'Cartalyst\Sentry\Facades\Laravel\Sentry');
$repoProvider = new ExtendedRepoServiceProvider($this->app); // This would be my new ExtendedRepoServiceProvider class
$repoProvider->register();
$formServiceProvider = new FormServiceProvider($this->app);
$formServiceProvider->register();
}
And then I would replace the service provider reference in my config/app.php file with the new ExtendedSentinelServiceProvider class:
'providers' => array(
...
'Sentinel\ExtendedSentinelServiceProvider',
...
),
My question is, would this make sense as a way of being able to store my modifications in my app directory rather than modifying the vendor code? Is this going to be a problem when it comes to name spacing? Is there a better way of doing this??
Any help would be greatly appreciated.
You shouldn't run into name spacing issues. However another option - especially if you're adding a feature that might be generally useful for others - would be to fork the project on github and then include a reference to your git project in the "repositories" attribute of your composer.json. For example you could add:
"repositories": [
{
"type": "git",
"url": "git#github.com:{your-user}/Sentinel.git"
}
],
And then composer would look to your fork when ever it does an install/update.