Laravel 3: Class not found in namespace - namespaces

This is the problem: I haven't be able to load a class from a namespace.
I'm developing a restful app and I'm trying to follow the Entity/Service/Repository way to access and give format to the requested data. The thing is that I cannot load any class from Services. I have created a folder inside my app called api, and within it the others 3 folders: api/entities/, api/services/ and api/repositories/. There are 2 more folders inside services: validators and datapickers.
As it is a RESTulf app, I also created an api folder inside the controllers folde: controllers/api/.
Here is the the current tree of my app folder:
app/
api/
entities/
repositories/
services/
datapickers/
MemberData.php
ConsumeData.php
validators/
...
models/
controllers/
api/
members.php
(other restful controllers)
languages/
...
In first instance, this is my Autoload section from start.php:
Autoloader::directories(array(
path('app').'models',
path('app').'libraries',
path('app').'api'
));
Autoloader::namespaces(array(
'Api' => path('app').'api',
));
And this is MemberData.php:
<?php
namespace Api\Services\Datapickers;
use Api\Repositories as Repo;
class MemberData
{
/* Do some stuff */
}
Now, when I try to use MemberData in controllers/api/members.php:
<?php
use Api\Services\Datapickers\MemberData;
class Api_Members_Controller extends Api_Base_Controller
{
public function get_index($id = null)
{
if (!empty($id))
$this->params->data = MemberData::getById($id);
else
{
$this->params->data = MemberData::getAll(
Input::get('offset'),
Input::get('limit')
);
}
return Response::json($this->params, 200);
}
}
I get the following:
Unhandled Exception
Message:
Class 'Api\Services\Datapickers\MemberData' not found
Location:
/path_to_project/application/controllers/api/members.php on line 15
which is this line: $this->params->data = MemberData::getById($id);
I autoload the api folder and register the base api namespace in start.php but I still keep receiving the same error. IT's like Laravel doesn't recognize the api namespace or something like that.
I tried to register the full namespace:
Autoloader::namespaces(array(
'Api\Services\Datapickers' => path('app').'api/services/datapickers',
));
but the error I got was: Call to undefinde method 'Api\Services\Datapickers\MemberData::getById()'. This was just a test (I don't want to register every sub-namespace in the Autoloader).

Related

Include helper class in view

I am working on a requirement of where I have to include all common methods like pagination, etc. which were used in my views into all my views. For this purpose I thought helper file is useful and created helper file in common\helpers\ directory with name Common as helper file name. I am facing difficulty in using this helper file in my view file.
I have included this helper file in my view as
use common\helpers\Common;
When I open the page I am getting error as "Class 'common\helpers\Common' not found"
My helper file: Common.php
namespace common\helpers;
class Common
{
protected $_file;
protected $_data = array();
public function __construct($file)
{
$this->_file = $file;
}
public static function getCommonHtml($id=NULL)
{
----
----
}
-----
--- Some other methods---
-----
}
I googled for this & got few solutions but they never worked.
You need to declare your new namespace in your composer.json:
"autoload": {
"psr-4": {
...
"common\\": "common/"
}
},
And the run:
composer dump-autoload
Alternatively you could declare alias for new namespace, so Yii autoloader will handle it (like in advanced template):
Yii::setAlias('#common', dirname(__DIR__))
But Yii autoloader will be dropped in Yii 2.1, so I would stick to composer-way (or do both - alias may be useful not only for autoloading).

How can I configure webpack + aurelia to use server-side templates

I'm trying to get my app to use server-side templates(.cshtml) and have tried the recommended approach of overriding the ViewLocator.prototype.convertOriginToViewUrl.
Unfortunately it doesn't seem to work with webpack, as hinted by #EisenbergEffect. on my previous question
I have a route that returns html and I want to use that as my view.
Example here in ASP.NET MVC
public ActionResult Template(string view)
{
return View(string.Format("~/Views/Shared/ClientTemplates/{0}.cshtml", view));
}
Aurelia code
import {ViewLocator} from 'aurelia-framework';
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging();
ViewLocator.prototype.convertOriginToViewUrl = (origin) => {
return "/common/template?view=" + origin.moduleId;
};
aurelia.start().then(a => a.setRoot());
}
Result Error: Cannot find module './/common/template?view=app'.
Running the route /common/template?view=app in a browser returns, as expected, the markup that resides in the app.cshtml
How can I configure webpack + aurelia to use server-side templates?

how to used custom widget in yii2 advanced template

i make simple custom Hellowidget .
HelloWidget class and file put in "components" directory and component directory put in "root directory in application"
`
public $message;
public function init()
{
parent::init();
if ($this->message === null) {
$this->message = 'Hello World';
}
}
public function run()
{
return Html::encode($this->message);
}
`
when this widget call in views
\app\components\HelloWidget::widget(['message' => 'Good morning'])
so i am getting error "Class 'app\components\HelloWidget' not found" namespace add still getting error
any idea about that widget
Hello i found solution
if you work on yii2 advanced template so components can`t put in "root directory" if you put so class not found error get
so solution is if you work on "frontend" so components put in that directory then used custom widget it will works ;)

Upgrading Laravel 4.2 to 5.0, getting [ReflectionException] Class App\Http\Controllers\PagesController does not exist

I was updating my project from laravel 4.2 to laravel 5.0. But, after I am facing this error and have been trying to solve it for the past 4 hours.
I didn't face any error like this on the 4.2 version. I have tried composer dump-autoload with no effect.
As stated in the guide to update, I have shifted all the controllers as it is, and made the namespace property in app/Providers/RouteServiceProvider.php to null. So, I guess all my controllers are in global namespace, so don't need to add the path anywhere.
Here is my composer.json:
"autoload": {
"classmap": [
"app/console/commands",
"app/Http/Controllers",
"app/models",
"database/migrations",
"database/seeds",
"tests/TestCase.php"
],
Pages Controller :
<?php
class PagesController extends BaseController {
protected $layout = 'layouts.loggedout';
public function getIndex() {
$categories = Category::all();
$messages = Message::groupBy('receiver_id')
->select(['receiver_id', DB::raw("COUNT('receiver_id') AS total")])
->orderBy('total', 'DESC'.....
And, here is BaseController.
<?php
class BaseController extends Controller {
//Setup the layout used by the controller.
protected function setupLayout(){
if(!is_null($this->layout)) {
$this->layout = View::make($this->layout);
}
}
}
In routes.php, I am calling controller as follows :
Route::get('/', array('as' => 'pages.index', 'uses' => 'PagesController#getIndex'));
Anyone please help. I have been scratching my head over it for the past few hours.
Routes are loaded in the app/Providers/RouteServiceProvider.php file. If you look in there, you’ll see this block of code:
$router->group(['namespace' => $this->namespace], function($router)
{
require app_path('Http/routes.php');
});
This prepends a namespace to any routes, which by default is App\Http\Controllers, hence your error message.
You have two options:
Add the proper namespace to the top of your controllers.
Load routes outside of the group, so a namespace isn’t automatically prepended.
I would go with option #1, because it’s going to save you headaches in the long run.

Tracking data on each request in Yii2

What will the best place in the code to track user's last visit date or any data that should be tracked on each request to application? Is it good idea to extend yii\web\Controller?
You can use a base controller and of course it is a good idea. But there is another approach that is more elegant. You can do like below:
1 - Add a component into your components directory, for example(MyTrackingClass):
namespace app\components;
class MyTrackingClass extends \yii\base\Component{
public function init() {
//SOME CODE HERE
//SOME CODE HERE
//SOME CODE HERE
parent::init();
}
}
2 - Add MyTrackingClass component into your components array in config file:
'components' => [
'MyTrackingClass'=>[
'class'=>'app\components\MyTrackingClass'
],
//other components
3 - Add MyTrackingClass into bootstarp array in config file:
'bootstrap' => ['log','MyTrackingClass'],
Now, you can see everything you wrote in your init() method, will be executed in every request, in every module, controller, action and so on...
Please note that, if you do not need to use Events and Behaviors you can use \yii\base\Object instead of \yii\base\Component