i am a beginner to laravel 4 framework.can any body help me why the following code showing an error when i am running it in the browser.
authors.php in controller
<?php
class Authors_Controller extends Base_Controller{
public $restful = true;
public function get_index(){
return View::make('authors.index');
}
}
routes.php
Route::get('authors',array('uses' => 'authors#index'));
can anybody suggest me a good video tutorial to learn laravel 4 clearly
You are extending the wrong class, based on the fact you are using laravel version 4.
Docs: http://laravel.com/docs/controllers#basic-controllers
class UserController extends BaseController {
Related
Peeps, I'm lost. Tried everything and after 5 hours of searching through the 10th page of Google hits, I give up. Maybe I just dont know how to ask Google the correct keywords..
I have this scenario: In lumen app, lets call it X, I have require custom packages CRUD and Storage, Storage is using functionality of CRUD.
StorageService has:
use Crud\Services\BaseService;
class StorageService extends BaseService{}
And Crud\BaseService has constructor, that uses Model:
use Illuminate\Database\Eloquent\Model;
class BaseService
{
protected $model;
public function __construct(Model $model)
{
$this->model = $model;
}
}
When I try to do anything with my app X, I get error:
Target [Illuminate\Database\Eloquent\Model] is not instantiable while building [Lumee\Storage\Services\StorageService]
I cannot get my head around how to get to proper class of Model, since I saw, that Model is abstract class.
Also, I'm using this CRUD package successfully in another App, only difference is, there CRUD is used directly in app, not via some other package. I'm confused, why there is working without any additional bindings and service registering..
EDIT: Added some binding into StorageServiceProvider (boot and register methods):
$this->app->bind(BaseService::class, function(){
return new BaseService(new Model());
});
And registered StorageServiceProvider in my boostrap/app.php:
$app->register(Storage\Providers\StorageServiceProvider::class);
Thing still returns same error. I tried with binding in CrudServiceProvider, nope.
you can't get object from abstract class (Model class) to solve this try this :
use Illuminate\Database\Eloquent\Model;
class BaseService
{
protected $model;
}
suppose your model is (Storage) :
use Crud\Services\BaseService;
class StorageService extends BaseService{
public function __construct(Storage $model)
{
$this->model = $model;
}
}
I'm curious is that possible to pass (as type-hinted) of custom class that extends FormRequest to be passed in to action within GET request ?
for example:
at routes/api.php
Route::get('/schema', '\App\Http\Controllers\TestController#getSchema');
then I have App\Http\Requests\SchemaRequest.php
and at controller, I want get this request from route within GET method.
class TestController extends Controller {
public function getSchema(\App\Http\Requests\SchemaRequest $request) {
// do other stuff here
}
}
I've tried to look deeper and doing some hack but nothing success yet?
Is that possible?
Any input would be appreciated, and thanks for reading
How about this workaround?
Route::get('/schema', 'TestController#getSchema',namespace =>'App\Http\Controller');
class TestController extends Controller {
public function getSchema(Request $request) {
// do other stuff here
}
}
In my Laravel 4 app, I have custom class sCAPTCHA in app/custom_libs/scaptcha/sCAPTCHA.php
class sCAPTCHA
{
public Create()
{
//.....some code
Session::put('sCAPTCHA_code',$code);
then in the controller ScaptchaController
$scaptcha = new sCAPTCHA();
$scaptcha->Create();
and Laravel 4 throw me an error:
Class 'scaptcha\Session' not found
It's very clear error, but how can I implement Session class of Laravel in my custom class sCAPTCHA?
I figured it out:
use Illuminate\Support\Facades\Session;
class sCAPTCHA
{
and now is working fine :)
How to call a model in laravel.
My code is:
use Jacopo\Authentication\Models\Guide;
class SampleController extends BaseController
{
public function index()
{
$model='Guide';
$guide=$model::where('guide_link','=',"guide")->get();
print_r($guide);
}
}
This will produce Class 'Guide' not found error.
If you added your class you should run in terminal
composer dump-autoload
to update your class map. Otherwise autoloader may not "see" your class and you are getting this error.
You need to add the namespace to your string:
class SampleController extends BaseController
{
public function index()
{
$model='Jacopo\Authentication\Models\Guide';
$guide=$model::where('guide_link','=',"guide")->get();
print_r($guide);
}
}
You could also resolve it from the IoC container, but you need to register it first:
App::bind('Guide', 'Jacopo\Authentication\Models\Guide');
And then you should be able to:
$model = App::make('Guide');
$guide = $model::where('guide_link','=',"guide")->get();
But this is not a very good option
I have the below code in the controller.
class Authors_Controller extends Base_Controller {
public $restful = true;
public function get_index() {
return View::make('authors.index');
}
}
When i run in the browser, i am getting the error,
class 'base_controller' not found
Change
class Authors_Controller extends Base_Controller {
to
class AuthorsController extends BaseController {
Edit: you might need to leave it as "Authors_Controller" depending how your file is named - but you should change it to the Laravel 4 convention of "AuthorsController"
In addition to The Shift Exchange's answer. You might also want to rename your function to getIndex, and rename your controller to AuthorsController as Laravel 4 is camelCased.