Laravel5 package view - php

I've crated package "za-web/tags"
composer.json:
{
"name": "za-web/tags",
"description": "",
"authors": [
{
"name": "Aleksandr Zamiatin",
"email": "cawa123#mail.ru"
}
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "5.0.*"
},
"autoload": {
"classmap": [
"src/views"
],
"psr-4": {
"ZaWeb\\Tags\\": "src/ZaWeb/Tags"
}
},
"minimum-stability": "dev"
}
And to view tags I'm trying to do
return View::make('tags::cloud', ['data' => $data])->render();
And I've got the error: No hint path defined for [tags].
It works only if I've add to TagsServiceProvider in register() method:
View::addNamespace('tags', __DIR__ . '/../../views');

Your solution work but as explained in the Laravel 5 documentation on package development you should add to your ServiceProvider's boot method:
public function boot()
{
$this->loadViewsFrom(__DIR__.'/path/to/views', 'courier');
}
instead of
View::addNamespace('tags', __DIR__ . '/../../views');
If you want to publish your package views, don't forget to use the publishes method:
public function boot()
{
$this->loadViewsFrom(__DIR__.'/path/to/views', 'courier');
$this->publishes([
__DIR__.'/path/to/views' => base_path('resources/views/vendor/courier'),
]);
}

Related

Laravel 8 package development - Route Target class does not exist

I know the title topic sounds similar to other questions, but I've searched many topics on stackoverflow and none resolve my issue.
I am currently developing a package under Laravel ^8.12, below the content of the function that register my routes:
protected function registerRoutes(): void
{
Route::prefix('workflowmakr')
->namespace('AlvariumDigital\WorkflowMakr\Http\Controllers')
->as('workflowmakr.')
->middleware(config('workflowmakr.routes_middleware'))
->group(__DIR__ . '/../routes/api.php');
}
And below is the content of the routes/api.php file:
<?php
use Illuminate\Support\Facades\Route;
Route::resource('actions', 'ActionController')->except(['created', 'edit']);
Route::resource('scenarios', 'ScenarioController')->except(['created', 'edit']);
Route::resource('statuses', 'StatusController')->except(['created', 'edit']);
Route::resource('transitions', 'TransitionController')->except(['created', 'edit']);
For a better view of the project architecture, below is a screenshot of the packages folder containing the package under development:
And finally, below is the composer.json declaring my package:
...
"extra": {
"laravel": {
"providers": [
"AlvariumDigital\\WorkflowMakr\\WorkflowMakrServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/",
"AlvariumDigital\\WorkflowMakr\\": "packages/AlvariumDigital/WorkflowMakr/src/"
}
},
...
When I execute the command php artisan route:list to view all my routes I got this error :
$> php artisan route:list
Illuminate\Contracts\Container\BindingResolutionException
Target class [AlvariumDigital\WorkflowMakr\Http\Controllers\ActionController] does not exist.
at D:\Films\R_D\Laravel packages\workflow-makr\vendor\laravel\framework\src\Illuminate\Container\Container.php:832
828▕
829▕ try {
830▕ $reflector = new ReflectionClass($concrete);
831▕ } catch (ReflectionException $e) {
➜ 832▕ throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
833▕ }
834▕
835▕ // If the type is not instantiable, the developer is attempting to resolve
836▕ // an abstract type such as an Interface or Abstract Class and there is
1 [internal]:0
Illuminate\Foundation\Console\RouteListCommand::Illuminate\Foundation\Console\{closure}(Object(Illuminate\Routing\Route))
2 D:\Films\R_D\Laravel packages\workflow-makr\vendor\laravel\framework\src\Illuminate\Container\Container.php:830
ReflectionException::("Class AlvariumDigital\WorkflowMakr\Http\Controllers\ActionController does not exist")
EDIT
Below is the content of the ActionController file:
<?php
namespace AlvariumDigital\WorkflowMakr\Http\Controllers;
use AlvariumDigital\Models\Action;
use AlvariumDigital\WorkflowMakr\Helpers\Constants;
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class ActionController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\JsonResponse
*/
public function index()
{
$query = Action::query();
if (config('workflowmakr.pagination_size') == -1) {
return response()->json($query->get(), 200);
}
return response()->json($query->paginate(config('workflowmakr.pagination_size')), 200);
}
// ...
}
EDIT 2
The content of the package composer.json file:
{
"name": "AlvariumDigital/WorkflowMakr",
"description": "Generalize the management of your workflows",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Alvarium Digital",
"email": "contact#alvariumdigital.com",
"homepage": "http://www.alvariumdigital.com",
"role": "Corporate"
},
{
"name": "EL OUFIR Hatim",
"email": "heloufir#alvariumdigital.com",
"homepage": "https://www.linkedin.com/in/eloufirhatim/",
"role": "Developer"
}
],
"support": {
"email": "heloufir#alvariumdigital.com"
},
"minimum-stability": "dev",
"require": {}
}
Did I do something wrong or incomplete?
You can ask for more details if needed.
Thanks
You have structured this directory incorrectly. You have the PSR4 autoloading loading the src directory as the namespace. Your controllers are not in the src folder, only the Service Provider is in there. So to composer there are no files for it to find and autoload based on your PSR4 autoloading.

Namespace issue in Model in Laravel - Package Development

I am developing a package in laravel which uses model for CRUD operations.
I have put it up in packagist as well, but when I try to install it in laravel application and visit a route defined by the package, it says
Class 'Zusamarehan\Tourify\Model\Tourifies' not found
The following is the folder structure of my package
rehan
tourify
src
assets
database
Http
Model
Tourifies.php
resources
routes
TourifyServiceProvider.php
composer.json
The following is the contents of my Tourifies.php
<?php
namespace Zusamarehan\Tourify\Model;
use Illuminate\Database\Eloquent\Model;
class Tourifies extends Model
{
}
The following is my composer.json file
{
"name": "zusamarehan/tourify",
"description": "A Package for adding Tour/Help to your Laravel Projects.",
"keywords": ["laravel", "tour", "tourify", "product-tour", "product-help"],
"type": "library",
"license": "MIT",
"authors": [
{
"name": "zusamarehan",
"email": "zrehan286#gmail.com"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=5.3.0"
},
"extra": {
"laravel": {
"providers": [
"Zusamarehan\\tourify\\TourifyServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Zusamarehan\\tourify\\": "src"
}
}
}
The Model class is not loading I suppose? I am not sure.
Can someone point out the mistake?
The namespaces in your classes use Zusamarehan\Tourify, however, in your composer.json you've used Zusamarehan\tourify. These should match.
You'll need to update your composer.json file so that the namespaces uses the correct case:
{
"name": "zusamarehan/tourify",
"description": "A Package for adding Tour/Help to your Laravel Projects.",
"keywords": ["laravel", "tour", "tourify", "product-tour", "product-help"],
"type": "library",
"license": "MIT",
"authors": [
{
"name": "zusamarehan",
"email": "zrehan286#gmail.com"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=5.3.0"
},
"extra": {
"laravel": {
"providers": [
"Zusamarehan\\Tourify\\TourifyServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Zusamarehan\\Tourify\\": "src"
}
}
}
"Zusamarehan\\tourify\\": "src" in your composer.json is wrong. Needs uppercase T. Looking at mine I also have a trailing / after src so you can try that as well.
You have the same lowercase t in the provider.

Fatal error when instanciating Pusher - Can't find class

I have a project to test and play around, with the following structure:
app/
controllers/
HomeController.php
handlers/
models/
vendor/
composer/
psr/
pusher/
pusher-php-server/
src/
Pusher.php
PusherException.php
PusherInstance.php
tests/
composer.json
autoload.php
index.php
I tried to require the Pusher autoloader in my index file:
require 'vendor/autoload.php';
Which is the following:
// autoload.php #generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInite16b90ab01042d2a69b1d54243c9e23a::getLoader();
Now, in my HomeController.php, I have the following code:
namespace App\controllers;
use \App\handlers\Views as View;
use \App\models\Home_model as Home;
use \App\controllers\SessionController;
use \Pusher\Pusher as Pusher;
class HomeController {
private $pusher;
public function __construct() {
$options = array(
'cluster' => 'hmm',
'encrypted' => true
);
$this->pusher = new Pusher(
'secret',
'secret',
'secret',
$options
);
}
public function index() {
$data['message'] = 'hello world';
$this->pusher->trigger('my-channel', 'my-event', $data);
return $this->view->render('views/home/index.php');
}
}
But this returns me an error:
Fatal error: Class 'Pusher\Pusher' not found in
And I'm not sure what I'm doing wrong. Could someone explain me what I'm doing wrong?
In composer.json I get the following:
{
"name": "pusher/pusher-php-server",
"description" : "Library for interacting with the Pusher REST API",
"keywords": ["php-pusher-server", "pusher", "rest", "realtime", "real-time", "real time", "messaging", "push", "trigger", "publish", "events"],
"license": "MIT",
"require": {
"php": "^5.4 || ^7.0",
"ext-curl": "*",
"psr/log": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8 || ^5.7"
},
"autoload": {
"psr-4": {
"Pusher\\": "src/"
}
},
"autoload-dev": {
"psr-4": { "": "tests/" }
},
"config": {
"preferred-install": "dist"
},
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
On Github, they mention that the library depends on cURL and JSON modules.
Not realy sure if this has something to do with my issue?
I'm still stuck, so any help is greatly appreciated.
Also, I'm using a .htaccess file, to rewrite my urls.
I have managed your code to work with right composer.json next to index.php:
{
"name": "awesome/project",
"type": "project",
"license": "MIT",
"authors": [
{
"name": "Author",
"email": "author#gmail.com"
}
],
"autoload": {
"psr-4": {
"": ""
}
},
"require": {
"pusher/pusher-php-server": "dev-master"
}
}
Then just run composer install.
My index.php contents:
<?php
require 'vendor/autoload.php';
use app\controllers\HomeController;
$ctrl = new HomeController();

How to use my Package in Laravel5

I have a question related to Package for Laravel 5. I am creating one package and tried to use that. package successfully created and using composer i can also get that in New Laravel setup , issue is that when i tried to use that it's says class not found. Here's my composer.json and Steps that i followed:
for e.g. my username = git_test and packagename = mypackage
My Package Structure :
**git_test > mypackage > src
My composer.json file
{
"name": "git_test/mypackage",
"description": "XXXXXXXXXX",
"keywords": ["laravel"],
"license": "MIT",
"authors": [
{
"name": "XXXXXXX",
"email": "XXXXXX#gmail.com"
}
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "5.0.*"
},
"autoload": {
"psr-4": {
"git_test\\mypackage\\": "src/"
}
},
"minimum-stability": "dev"
}
Here's my src/myclass.php
namespace git_test\mypackage;
class myclass {
function test(){ echo "This is Test"; }
}
Now i am going to use this in my new laravel project so i add package in my directory composer and try to use the myclass in my HomeController
HomeController Code
use git_test\mypackage\myclass as TaskClass;
class HomeController extends Controller {
public function index()
{
$atTaskObj = new TaskClass('');
}
I got the error like "git_test\mypackage\myclass" Not Found. where i am doing wrong? any suggestion please.
Thanks in Advance!!!
PSR-4 paths have to end with \\:
"autoload": {
"psr-4": {
"git_test\\mypackage\\": "src/"
}
},

Composer update not reflecting repository changes

I have the follow composer.json
{
"name": "mjohnson/transit",
"type": "library",
"description": "A file uploader, validator, importer and transformer library.",
"keywords": [
"transit", "file", "uploader", "validator", "importer", "transformer", "transporter",
"image", "audio", "video", "text", "application", "archive", "s3", "glacier"
],
"homepage": "http://milesj.me/code/php/transit",
"license": "MIT",
"authors": [
{
"name": "Miles Johnson",
"homepage": "http://milesj.me"
}
],
"require": {
"php": ">=5.3.0",
"ext-curl": "*",
"ext-mbstring": "*",
"aws/aws-sdk-php": "2.0.*"
},
"support": {
"source": "https://github.com/milesj/php-transit"
},
"autoload": {
"psr-0": { "Transit": "src/" }
}
}
when I run a composer update the source code is not updated to reflect current repository: https://github.com/milesj/transit
I tried to delete lock file whitout success. Tried composer [update|install}
For instance, my current (local) code:
src/Transit/File.php:
[...]
public function __construct($path) {
if (!file_exists($path)) {
throw new IoException(sprintf('%s does not exist', $path));
}
$this->_path = $path;
}
[...]
current repository code:
[...]
public function __construct($path) {
if (is_array($path)) {
if (empty($path['tmp_name'])) {
throw new IoException('Passing via array must use $_FILES data');
}
$this->_data = $path;
$path = $path['tmp_name'];
}
if (!file_exists($path)) {
throw new IoException(sprintf('%s does not exist', $path));
}
$this->_path = $path;
// #version 1.3.2 Rename file to add ext if ext is missing
if (!$this->ext()) {
$this->rename();
}
// #version 1.4.0 Reset the cache
$this->_cache = array();
}
[...]
You have the wrong composer.json. The one you mention is for the library named "mjohnson/transit" - if you are not developing this exact software, then this is wrong.
You should create a new composer.json file containing at least this line:
{ "require": { "mjohnson/transit" : "*" } }
Then run composer install.
I do not know what you did to get that composer.json file, but if you originally cloned that other repository, and now edit that file, things will break! Backup your code if any. Try to undo what you did wrong without undoing your own code.

Categories