I am trying to make a view on my package, and this is my code:
View::make("User::login");
But I get this error: No hint path defined for [User].
My structure is like this:
app
├──config
├──database
├── modules
└── Core
└── User
├──Controllers
├──models
└──views
└──login.blade.php
├── lang
├── migrations
└── routes.php
The view namespaces actually have nothing to do with PSR-4. You also have to add view directories manually. You can either do that by adding it to the paths array in config/view.php or by registering it somewhere else (preferably in a service provider)
View::addLocation('/path/to/views');
To come back to your actual question, you can register view namespaces like this:
View::addNamespace('User', '/path/to/views');
i've resolved my problem.
it was my fault, it should be :
View::make("Core/User::login");
thanks.
Related
The application layout with my composer package is as follows
some-application
├── index.php
└── composer.json
└── vendor/my/package
├── composer.json
└── src
├── Foobar
│ └── style.css.php
└── Bar
├── Moo.php
└── Baz.php
index.php uses style.css.php as a stylesheet in its html markup. The stylesheet is a php file because the styles need to get rendered dynamically. So, style.css.php is accessed directly by the clients browser, without passing index.php that includes the autoloader.
Now, I would like to access Moo and Baz in style.css.php, but what is the correct approach to define some kind of autoloader for my package that allows this?
I was only able to find info regarding the autoloader that the application would include. But what if I bypass the application's autoloader invocation?
Any advice is much appreciated, thanks.
To autoload composer package classes, you just need to somehow load the autoloader.
In your script, you can say:
<?php
$path = file_exists('vendor/autoload.php') ? 'vendor/autoload.php' : '../../../autoload.php';
require_once $path;
use Bar\Moo;
$moo = new Moo();
This will load the application's autoloader when the package is included in an application, or will load the package's own autoloader when you develop the package, e.g. running tests.
I'm working in a WordPress plugin, following the WordPress Coding Standards.
This is the current structure for the plugin:
.
├── includes
├── languages
├── views
├── class-plugin-admin.php
├── class-plugin.php
└── plugin.php
That said, I would like to use the Composer autoloader to load both classes (for unit testing purposes).
Today I've been loading them like this:
"autoload-dev": {
"files": [
"class-plugin.php",
"class-plugin-admin.php"
]
}
However, not sure if this one would be the best way to do it.
So here are the questions:
Should I move my classes to a subdirectory in the plugin?
Which one would be the best way to load them via composer?
Should I move my classes to a subdirectory in the plugin?
You don't need to, but you can. It definitely makes developing the plugin a lot easier, and I would suggest to do so.
As you indicated, you probably already have tests, so why not go ahead and adjust the structure to something like this:
.
├── includes
├── languages
├── src
│ ├── Plugin.php
│ └── PluginAdmin.php
├── test
│ ├── PluginAdminTest.php
│ ├── PluginTest.php
│ └── phpunit.xml
├── views
├── composer.json
└── plugin.php
Note I adjusted the class names, because ideally, you probably want to use some way of PSR-4 autoloading for your classes.
For reference, see:
https://www.smashingmagazine.com/2015/05/how-to-use-autoloading-and-a-plugin-container-in-wordpress-plugins/
Which one would be the best way to load them via composer?
This largely depends on whether you want to move your classes to a sub-directory or not. Let's assume you did, and let's assume you were using Toally\Amazing as a namespace prefix for your classes:
PSR-4
Use the PSR-4 autoloader:
{
"autoload-dev": {
"Totally\\Amazing\\": "src/"
}
}
For reference, see:
https://getcomposer.org/doc/04-schema.md#psr-4
Classmap
List the directory which contains the classes:
{
"autoload-dev": {
"classmap": [
"src/",
]
}
}
For reference, see:
https://getcomposer.org/doc/04-schema.md#classmap
Decide for yourself which makes the most sense for you!
Autoloading in production
As you can see, I use the autoload-dev section to indicate that composer autoloading is only relevant for development, not for production. You would still need to
require classes in plugin.php or
implement autoloading for your classes in plugin.php
Take a look at
https://www.smashingmagazine.com/2015/05/how-to-use-autoloading-and-a-plugin-container-in-wordpress-plugins/
which provides a corresponding example for autoloading classes from your plugin.
I have an old application running on top of Zend Framework 1 (ZF1), I'm starting a Symfony3 (SF) migration so I have managed with some .htaccess rules to make Symfony3 work inside the ZF1 application. Basically if I call a Symfony route it goes to Symfony controllers otherwise goes to ZF1 controllers.
I need to use ZF1 session data in the SF controller but the ZF1 Session and SF Session works completely in different way so calling ZF1 Session from the SF won't work. (I've already test it).
What comes to my mind then was save the same data to both session objects: the ZF1 used all over the legacy application and the SF used on the new controllers so I end up with something like this:
// The following goes to an special ZF1 storage managed through Session
$this->view->user = Zend_Auth::getInstance();
$result = $this->view->user->getStorage()->read();
// This is a Symfony\Component\HttpFoundation\Session\Session
$session_1 = new Session(new PhpBridgeSessionStorage());
$session_1->start();
$session_1->set('result', $this->view->user->getStorage()->read());
// here $session_1 holds the values properly
$session_1->get('result');
The Symfony session is configured as follow:
session:
handler_id: session.handler.native_file
save_path: '%kernel.root_dir%/sessions'
Then in my Symfony controller I am trying to access the stored data as:
$session = new Session();
$result = $session->get('result');
But the value of $session->get('result') is null and as far as I know it shouldn't be empty. I don't know if the directory structure is responsible for this issue or what I am doing wrong or maybe is simple and this is not possible at all because the Session() objects instances are different due to the directory structure.
This is how the directories are structured (weird but is the only way I did found so far to make this work):
├── application
│ ├── api
│ ├── controllers -> ZF1 controllers
│ ├── forms
│ ├── layout
│ ├── models
│ ├── soap
│ └── views
...
├── oneview_symfony -> this is the SF app
│ ├── app
...
│ ├── var
│ ├── vendor -> it has his own /vendor folder and a /symfony libraries
│ └── web
...
└── vendor
...
├── symfony -> /symfony libraries are here as well
...
├── zendframework
Update
Didn't tried before but now following #bishop suggestion I have bridged the Symfony session with ZF1 session (or at least I think I did) by adding the following:
session:
storage_id: session.storage.php_bridge
handler_id: session.handler.native_file
save_path: '%kernel.root_dir%/sessions'
But it doesn't work either, the error is same as before result gets null.
Any clues? Ideas?
I agree with #bishop and you correctly bridged the session, but I believe your problem is different. My assumption is that your session save_path is not shared between the Zend and Symfony applications.
You have to use the same path for this to work.
Try setting:
phpSettings.session.save_path = "/path/to/symfony/kernel/root/sessions"
in your application.ini of the Zend framework application.
Replace with your sessions path.
I'd like to make my first large project in php. I use Phalcon PHP and I created project structure using Phalcon Developer Tools. It something like:
.
├── app
│ ├── cache
│ ├── config
│ │ ├── config.php
│ │ ├── loader.php
│ │ └── services.php
│ ├── controllers
│ ├── migrations
│ ├── models
│ └── views
├── index.html
└── public
├── css
├── files
├── img
├── index.php
├── js
└── temp
I think i'll need some global function and classes. I'd like to implement for example Laravel's dd function to dumb variables and using this function like
dd($value);
wherever i want. I also want to create some global classes to use theirs static functions. For example:
User::isLogged()
How to implement this in my project? Create directory functions or lib or indcude in app/? Is it a convention? Place global classes in individual folders? How to separate global functions and classes and register those in standard Phalcon loader and do it once for whole project?
Good thing about Phalcon is that you have the freedom to organize your project in the way it best fits your current situation.
A more general approach I'm using in most of my projects is to register the most used namespaces in the autoloader. In my case I'm using multi module structure and this is done in the Module.php file for the given module.
Module class:
class Module
{
public function registerAutoloaders($di)
{
$config = $di->getConfig();
$loader = new \Phalcon\Loader();
$namespaces = [
'Frontend\Controllers' => __DIR__ . '/controllers/',
'Frontend\Forms' => __DIR__ . '/forms/',
'Models' => $config->site->path->common . 'models/',
'Helpers' => $config->site->path->common . 'helpers/',
];
$loader->registerNamespaces($namespaces);
$loader->register();
}
}
Helpers in my case are files which are not models and serve for something specific. For example I have a Files helper which holds functions for manipulating the file system. I've got a Helper for handling string operations like slugalization, latinization and so on...
I've also have a Lib folder in which I put my public libraries like PHPMailer, BrowserDetect, ImageProcessing libraries and so on.
And now about the global functions like Laravel's dd(). I have a small file which I include in the bootstrap file or your index.php. It contains 1-2 global functions like:
function d($what)
{
echo '<pre>';
print_r($what);
die('</pre>');
}
In my case there are not that many global functions which I want to use everywhere easily, like above one for debugging. Rest of the stuff I've put in the Helper files mentioned above.
Hope I helped and would be glad to hear someone else's opinion on this.
I want to create an API to communicate with a REST Web Service.
For this, I'm building this API as a library. I Just create the folder structure:
├── composer.json
├── LICENSE
├── README.md
└── src
└── PkgRoot
└── PkgName
├── XXXAPIFactory.php
├── XXXAPI.php
├── XXXAPIRestImpl.php
├── XXXResponse.php
└── Protocol.php
Now, I'm trying add a configuration for the API-KEY. I believe this should be added in config.yml.
Should I change all these structure the be like a Symfony Bundle? How can I add and register configurations parameters in config.ym?
It is not mandatory, but I think that you definitely should. Why aren't you using a Bundle ?
Using a Bundle would permit you, among so many other cool things, to create a friendly configuration as you want.