Laravel 4 Eloquent Class not found - rest

I'm trying to learn a bit about Laravel4 framework, so I decided to setup a small restful connection with a mysql database using the
php artisan controller:make ProductsController.
Unfortunatelly after running the app I get this error message:
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Class 'Products' not found","file":"\/Applications\/MAMP\/htdocs\/shoppingCart\/app\/controllers\/ProductsController.php","line":14}}
anybody knows how can I fix that ? thank's in advance.
Here is the relevant code:
1) my route:
Route::get('/', function()
{ return View::make('home');});
Route::resource('products','ProductsController');
2) controller:
class ProductsController extends BaseController {
public function index()
{
return Products::all()->toArray();
}
etc...
3) model:
Class Products extends Eloquent{
protected $table = 'products';}
I've done also composer dump-autoload

Related

yii2 RestAPI modelClass value for model from other app

I am developing a RESTful API looking at the documentation from http://www.yiiframework.com/doc-2.0/guide-rest-versioning.html and http://budiirawan.com/setup-restful-api-yii2/.
I have a model (ActiveRecord) class called Client in common/models folder as
namespace common\models;
class Client extends ActiveRecord
{
...
}
Then, I have ClientController class in api/modules/v1/controllers folder as
namespace api\modules\v1\controllers;
class ClientController extends ActiveController {
public $modelClass = 'common\models\Client';
}
If I browse localhost/api/v1/clients I get "Class common\models\Client not found" error. I tried different versions of modelClass, but cannot get the answer.
Maybe I need to configure something extra? Any help is appreciated, thanks

Using my own service with Laravel4

In my app, I was testing Google Directions API with ajax, but since I was just testing all the logic was in the routes.php file. Now I want to do things the proper way and have three layers: route, controller and service.
So in the routes I tell Laravel which method should be executed:
Route::get('/search', 'DirectionsAPIController#search');
And the method just returns what the service is supposed to return:
class DirectionsAPIController extends BaseController {
public function search() {
$directionsSearchService = new DirectionsSearchService();
return $directionsSearchService->search(Input::all());
}
}
I created the service in app/libraries/Services/Directions and called it DirectionsSearchService.php and copied all the logic I developed in routes:
class DirectionsSearchService {
public function search($input = array()) {
$origin = $input['origin'];
$destination = $input['destination'];
$mode = $input['mode'];
// do stuf...
return $data;
}
}
I read the docs and some place else (and this too) and did what I was supposed to do to register a service:
class DirectionsAPIController extends BaseController {
public function search() {
App::register('libraries\Services\Directions\DirectionsSearchService');
$directionsSearchService = new DirectionsSearchService();
return $directionsSearchService->search(Input::all());
}
}
// app/libraries/Services/Directions/DirectionsSearchService.php
use Illuminate\Support\ServiceProvider;
class DirectionsSearchService extends ServiceProvider {
}
I also tried adding libraries\Services\Directions\DirectionsSearchService to the providers array in app/config/app.php.
However, I am getting this error:
HP Fatal error: Class
'libraries\Services\Directions\DirectionsSearchService' not found in
/home/user/www/my-app-laravel/bootstrap/compiled.php on line 549
What am I doing wrong? And what is the usual way to use your own services? I don't want to place all the logic in the controller...
2 main things that you are missing:
There is a difference between a ServiceProvider and your class. A service provider in Laravel tells Laravel where to go look for the service, but it does not contain the service logic itself. So DirectionsSearchService should not be both, imho.
You need to register your classes with composer.json so that autoloader knows that your class exists.
To keep it simple I'll go with Laravel IoC's automatic resolution and not using a service provider for now.
app/libraries/Services/Directions/DirectionsSearchService.php:
namespace Services\Directions;
class DirectionsSearchService
{
public function search($input = array())
{
// Your search logic
}
}
You might notice that DirectionsSearchService does not extend anything. Your service becomes very loosely coupled.
And in your DirectionsAPIController.php you do:
class DirectionsAPIController extends BaseController
{
protected $directionsSearchService;
public function __construct(Services\Directions\DirectionsSearchService $directionsSearchService)
{
$this->directionsSearchService = $directionsSearchService;
}
public function search()
{
return $this->directionsSearchService->search(Input::all());
}
}
With the code above, when Laravel tries to __construct() your controller, it will look for Services\Directions\DirectionsSearchService and injects into the controller for you automatically. In the constructor, we simply need to set it to an instance variable so your search() can use it when needed.
The second thing that you are missing is to register your classes with composer's autoload. Do this by adding to composer.json's autoload section:
"autoload": {
"classmap": [
... // Laravel's default classmap autoloads
],
"psr-4": {
"Services\\": "app/libraries/Services"
}
}
And do a composer dump-autoload after making changes to composer.json. And your code should be working again.
The suggestion above can also be better with a service provider and coding to the interface. It would make it easier to control what to inject into your controller, and hence easier to create and inject in a mock for testing.
It involves quite a few more steps so I won't mention that here, but you can read more in Exploring Laravel’s IoC container and Laravel 4 Controller Testing.

Error in using my own Curl class in laravel

i have laravel 4 installed in my wamp server. this what i did :
1-add this "app/classes" to composer.json.
2-create folder classes in app and put Curl.php class in that folder.
3-add this app_path().'/classes', to global.php inside app/start.
4-run composer dump-autoload in command in www directory.
5-for using like Curl::help() must add this alias to app/config/app.php aliases section 'Curl'=>'Curl' .
after doing this when i return return Curl::hello(); in router this page comes :
http://www.mediafire.com/view/h9489jr5s2699ty/err.PNG
my Curl's class : Curl class
any help??
This is not how Laravel aliases works, you need more code (create Facades and Service Providers) to make it work.
So you have some options:
1) Remove the Alias from app/config/app.php and instantiate your class:
$curl = new Curl;
$curl->help();
2) Instantiate your class and bind it to the IoC container, in global.php, filters.php or create a file for that:
App::bindShared('mycurl', function($app)
{
return new Curl;
});
And create a Facade:
<?php namespace MyClasses\Facades;
use Illuminate\Support\Facades\Facade;
class MyCurlFacade extends Facade {
protected static function getFacadeAccessor()
{
return 'mycurl';
}
}
Your Alias has to point to this Facade script file, like all the others you see in app.php.
'Curl' => 'MyClasses\MyCurlFacade',
And it should work like this Curl::hello();.
3) Create the usual (correct?) Laravel structure, which also includes a ServiceProvider to instantiate your class and bind it to the IoC container in the application Boot:
<?php namespace MyClasses;
use Illuminate\Support\ServiceProvider;
class MyCurlServiceProvider extends ServiceProvider {
protected $defer = false;
public function boot()
{
}
public function register()
{
{
$this->app['mycurl'] = $this->app->share(function($app)
{
return new MyCurl;
});
}
public function provides()
{
return array('mycurl');
}
}
THIS IS UNTESTED CODE, SO DO NOT EXPECT IT TO WORK IN THE FIRST RUN

How to add your own library to Zend Framework

So i have been designig an application to run on the Zend Framework 1.11 And as any programmer would do when he sees repeated functionalities i wanted to go build a base class with said functionalities.
Now my plan is to build a library 'My' so i made a folder in the library directory in the application. So it looks like this
Project
Application
docs
library
My
public
test
So i created a BaseController class in the My folder and then decided to have the IndexController in my application extend the BaseController.
The Basecontroller looks like this :
class My_BaseController extends Zend_Controller_Action
{
public function indexAction()
{
$this->view->test = 'Hallo Wereld!';
}
}
And the IndexController looks like this :
class WMSController extends My_BaseController
{
public function indexAction()
{
parent::indexAction();
}
}
As adviced by a number of resources i tried adding the namespace for the library in the application.ini using the line
autoloadernamespaces.my = “My_”
But when i try to run this application i recieve the following error
Fatal error: Class 'My_BaseController' not found in
C:\wamp\www\ZendTest\application\controllers\IndexController.php
Am i missing something here? Or am i just being a muppet and should try a different approach?
Thanks in advance!
Your original approach will work for you in application.ini, you just had a couple of problems with your set up.
Your application.ini should have this line:-
autoloadernamespaces[] = "My_"
Also, you have to be careful with your class names, taking your base controller as an example, it should be in library/My/Controller/Base.php and should look like this:-
class My_Controller_Base extends Zend_Controller_Action
{
public function indexAction()
{
$this->view->test = 'Hello World!';
}
}
You can then use it like this:-
class WMSController extends My_Controller_Base
{
public function indexAction()
{
parent::indexAction();
}
}
So, you had it almost right, but were missing just a couple of details. It is worth getting to know how autoloading works in Zend Framework and learning to use the class naming conventions
I don't know about .ini configuration, but I add customer libraries like this (index.php):
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance()->registerNamespace('My_');

Zend Framework and CodeIgniter Model class doubts

Both zend and CI framework have seperate Model, View, Controller directories. My doubt is how to use model in Zend Framework.
[ CodeIgniter Controller ]
<?php
class Users extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('users_model');
}
function index()
{
// here i am using model function //
$myvar = $this->user_model->login();
}
}
?>
[ Zend Framework Controller ]
<?php
class UsersController extends Zend_Controller_Action
{
public function indexAction()
{
// how to load model and use here ???? //
}
}
?>
In CodeIgniter Controller I load model "users_model" and used in index function. In the same way how to create zend model and use in controller? please help me, sorry my english is not good.
Thanks friends,
Rajendra
You shouldn't compare Zend with CodeIgniter since the philosophy is quite different.
It is best to read:
http://framework.zend.com/manual/en/learning.quickstart.create-model.html