I have a folder structure like this:
Controller/
├── API/
│ ├── APIController.php
│ └── APIFunction.php
├── OTPController.php
├── LoginController.php
└── HomeController.php
I'm trying to call a method from OTPController.php within my APIController.php:
$otpController = new App\Http\Controllers\OTPController();
But it returns an error:
Class 'App\Http\Controllers\API\App\Http\Controllers\OTPController' not found in file /var/www/app/Http/Controllers/API/APIController.php
Related
I've used autoloader from composer. Everything is fine when I set class name directly
<?php
namespace Matrix;
$router = new Router;
$object = $router->object;
$method = $router->method;
$id = $router->id;
$action = new Accounts();
It works. But if I try to set variable as a class name
$action = new $object();
it returns Fatal error: Uncaught Error: Class 'Accounts' not found.
var_dump($action);
returns string 'Accounts' (length=8)
Section autoload in my composer.json:
"autoload": {
"psr-4": {
"Matrix\\": "matrix/controllers/"
}
My directories structure:
├── matrix
│ ├── controllers
│ │ ├── Accounts.php
│ │ ├── BaseController.php
│ │ ├── OffersController.php
│ │ ├── RefreshController.php
│ │ └── Router.php
│ ├── index.php
I have the following directory structure in a Zend Framework 1 application:
application/
├── controllers/
│ └── admin/
│ └── TaxRateController.php
│ └── MainInitController.php
I am trying to access taxrate which should be indexAction() but I am doing something wrong since I am getting a Zend_Controller_Action_Exception. This is what I have tried all this URL combination so far:
http://localhos/admin/tax-rate/index
http://localhos/admin/tax-rate
http://localhos/admin/taxrate
http://localhos/admin/taxrate/index
And all of them generates the same error:
[message:protected] => Action "taxRate" does not exist and was not
trapped in __call()
This is the content of the class(es):
class TaxRateController extends MainInitController
{
public function indexAction()
{
echo 'I am here'; die();
}
}
class MainInitController extends Zend_Controller_Action {
....
}
What I am missing here? How I should be calling the controller/action?
Update 1:
I have tried to move the directory outside controllers but the result is the same:
application/
│ └── admin/
│ └── TaxRateController.php
├── controllers/
│ └── MainInitController.php
I am calling as http://localhost/admin/taxrate in this scenario.
With basic structure it will take time and effort to do that but it can be done
application/
├── controllers
│ └── admin
│ └── TaxRateController.php
You need to create routes for every controller under sub directory in your bootstrap:
public function _initAdminRoute()
$router = Zend_Controller_Front::getInstance()->getRouter();
// structure
$router->addRoute(
'unique_route_name',
new Zend_Controller_Router_Route('/admin/controllerRoute/:action/*',
['controller' => 'subdirName_controllerRoute']
)
);
// Like this
$router->addRoute(
'admin_taxrate_route',
new Zend_Controller_Router_Route('/admin/tax-rate/:action/*', ['controller' => 'admin_tax-rate'])
);
}
After this you need to rename your controller classes with subdirectory name to let zend find them. But do not change controller file names.
class TaxRateController => class Admin_TaxRateController
Now you can use your controllers, but a little fix may be needed for your views cause right now zend can not find your view directory. You need to move all your admin views to admin subdirectory or it will throw an error something similar to this.
Fatal error: Uncaught exception 'Zend_View_Exception' with message 'script 'admin/tax-rate/action.phtml' not found in path (application/views/scripts/)' in
Hope this helps, but still i will recommend using module structure.
If 'admin' is a module you should use a directory structure like this:
application/
├── modules
│ └── admin
| └── controllers
│ └── TaxRateController.php
Also make sure your application/configs/application.ini is according with it:
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules/"
Edit
Be sure your file public/index.php ends like this:
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
The PHPUnit manual highlights some conventions:
The tests for a class MyClass go into a class MyClassTest
The class MyClassTest live in file MyClassTest.php
MyClassTest inherits from PHPUnit_Framework_TestCase
Tests are public methods that are named test*
This will result in something like this folder structure:
├── src/
│ ├── classes/
│ │ ├── MyClass.php # Different
│ └── ...
├── tests/
│ ├── testcases/
│ │ ├── MyClassTest.php # Different
│ ├── bootstrap.php
│ └── ...
└── ...
... and this test case:
MyClassTest extends PHPUnit_Framework_TestCase {
testMyMethod() {
// Code here.
}
}
My question
I'm wondering if there is any reason why the naming used inside the test suite can't mirror the project's source code? For example, I'm thinking file names could match:
├── src/
│ ├── classes/
│ │ ├── MyClass.php # Same
│ └── ...
├── tests/
│ ├── testcases/
│ │ ├── MyClass.php # Same
│ ├── bootstrap.php
│ └── ...
└── ...
And if using PHP > 5.3, namespaces can be used to allow class names to match:
namespace MyProject\MyTests;
MyClass extends PHPUnit_Framework_TestCase { # The class name MyClass matches the class name used in my project's source.
/**
* #test
*/
MyMethod() { # The method name MyMethod matches the method name used in my project's source.
// Code here.
}
}
Note the #tests annotation is used so method names can match.
And if using PHP > 5.3, namespaces can be used to allow class names to match:
There are reasons not to do this:
It makes sense to have test and class under test in the same namespace
Otherwise you need to import the class under test with a class alias to distinguish it from the test case:
use MyProject\MyClass as MyActualClass;
The method name MyMethod matches the method name used in my project's source.
This might sound appealing if you think of testMyMethod as the alternative, but this is not the convention. Instead you should use more descriptive test method names like testThatMyMethodReturnsTrueIfFooIsBar.
I'm developing a web service using Slim Framework 2, it's the first time I use php. I'm having an issue with the Slim object:
There's my code:
require 'db.php';
require 'vendor/slim/slim/Slim/Slim.php';
echo 'require ok';
$app = new vendor/slim/slim/Slim();
echo '$app ok';
The 'require' lines are ok. But he $app line doesn't work I use the same path to the Slim.php file and also I've tryed to use this path:
$app = /slim/Slim();
The first echo is executing but the second one is not working. My ws.php file is on the same directori as the vendor folder:
vendor/slim/slim/Slim/
├── Environment.php
├── Exception
│ ├── Pass.php
│ └── Stop.php
├── Helper
│ └── Set.php
├── Http
│ ├── Cookies.php
│ ├── Headers.php
│ ├── Request.php
│ ├── Response.php
│ └── Util.php
├── Log.php
├── LogWriter.php
├── Middleware
│ ├── ContentTypes.php
│ ├── Flash.php
│ ├── MethodOverride.php
│ ├── PrettyExceptions.php
│ └── SessionCookie.php
├── Middleware.php
├── Route.php
├── Router.php
├── Slim.php
└── View.php
Is $app = new vendor/slim/slim/Slim(); correct? I'm lost about this topic.
Solved using Frank Martin solution:
require 'vendor/autoload.php';
$app = new \Slim\App;
Instead:
require 'vendor/slim/slim/Slim/Slim.php';
$app = new vendor/slim/slim/Slim();
Regards
Make sure you've installed the composer dependencies then use this instead.
require 'vendor/autoload.php';
$app = new \Slim\App;
I want to be able to run some PHP code in my mostly static webpage, my Parse project looks like this:
.
├── LICENSE
├── README.md
├── cloud
│ └── main.js
├── config
│ └── global.json
└── public
├── confirmation.html
├── contact.php
├── contactsettings.php
├── css
├── experiencias
├── fonts
├── img
├── index.html
├── js
├── redirect.html
├── subscribe.php
└── thanks.html
So as you can see, I have some PHP code to run, yet when trying to, Parse gives me the following error:
Page not found
This Parse App does not have a page at the requested URL.
Like mentioned in a comment by #nathancahill, Parse.com STATIC webhosting does not support PHP code.
PHP code is dynamic and not static content.
To host a dynamic website on Parse, you can use Cloud Code or Express.
Read more here: https://parse.com/docs/hosting_guide#webapp