Scaffolding with Laravel - php

I am relativley new to laravel, and have created an 'article' scaffold using this plugin:
https://github.com/JeffreyWay/Laravel-4-Generators
And running:
php artisan generate:resource article --fields="title:string, body:text"
Everything works out fine, with the table being created in my database and the associated files appearing in my project directory. However, when I navigate to localhost/laravel/public/articles (my directory), I get the following error:
ErrorException (E_NOTICE)
HELP
Undefined offset: 1
Open: C:\xampp\htdocs\laravel\vendor\laravel\framework\src\Illuminate\Routing\Router.php
$route = $this->current();
$request = $this->getCurrentRequest();
// Now we can split the controller and method out of the action string so that we
// can call them appropriately on the class. This controller and method are in
// in the Class#method format and we need to explode them out then use them.
list($class, $method) = explode('#', $controller);
return $d->dispatch($route, $request, $class, $method);
I tried running
php artisan optimize --force
but this didn't help.
Any advice?

you must add route for article like this:
Route::resource('articles', 'ArticlesController');
in app/routes.php file.

Related

Change the routes for models in laravel 8 php artisan auth

I am making application with tutorial. At the very beginning, I ran the command php artisan ui:auth which created a login and registration page for me. Initially my models (eg Users) were in the App/Models/Users folder but I had to change their location to /App/Users because only then application it worked according to the tutorial. The login page itself works, but when I click the 'login' button I get an error:
Error
Class '\App\Models\User' not found
Illuminate\Auth\EloquentUserProvider::createModel
C:\xampp\htdocs\testowa4\vendor\laravel\framework\src\Illuminate\Auth\EloquentUserProvider.php:186
186 line is in the function:
public function createModel()
{
$class = '\\'.ltrim($this->model, '\\');
return new $class; //186 line
}
How to change model paths for these files? I need them outside the Models folder. Is there any way to refresh the routes or something like that?

Controllers not working on Laravel 8 despite uncommenting in RouteServiceProvider

I try to create routes to my index.blade.php page, i've made a controller "ProductController" using cmd php artisan make:controller ProductController, so in http --> Controllers i do have a ProductController.php file and i put this code in it :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductContoller extends Controller
{
public function index()
{
return view('products.index');
}
}
and then in my web.php i create a route using this code
Route::get('/boutique', 'ProductController#index');
However it doesn't work.
Firstly, when i go to the pretty url i've setup for my project at localhost using Laragon --> Projetname.test i get the normal Laravel Welcome page, but when i try to go to the url i've just setup like : ProjectName.test/boutique, i get
"Target class [App\Http\Controllers\ProductController] does not exist."
After reading about the changes since the update of Laravel to V8 here, i've seen that the update made some requirements for routes since $namespace prefix are not enabled automatically, but however that can be enabled again by uncommenting this line in RouteServiceProvider
// protected $namespace = 'App\\Http\\Controllers';
I do uncommenting that line and then clear the cache using php artisan route:cache, however it still not working..
When i first started doing research about routes issues in Laravel i've seen many forum spotted out that apache Allowoverride settings in httpd.config file may cause issue, so i change it settings from None to All and then restart Laragon but nothing works.
After correcting label on my controller it still do not work, i try both methode (the old and the new one) none of them works for me, cmd keeps returning me :
λ php artisan route:list
Illuminate\Contracts\Container\BindingResolutionException
Target class [ProductController] does not exist.
at C:\laragon\www\ProjectName\vendor\laravel\framework\src\Illuminate\Container\Container.php:835
831▕
832▕ try {
833▕ $reflector = new ReflectionClass($concrete);
834▕ } catch (ReflectionException $e) {
➜ 835▕ throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
836▕ }
837▕
838▕ // If the type is not instantiable, the developer is attempting to resolve
839▕ // an abstract type such as an Interface or Abstract Class and there is
1 [internal]:0
Illuminate\Foundation\Console\RouteListCommand::Illuminate\Foundation\Console\{closure}(Object(Illuminate\Routing\Route))
2 C:\laragon\www\ProjectName\vendor\laravel\framework\src\Illuminate\Container\Container.php:833
ReflectionException::("Class "ProductController" does not exist")
Laravel 8 Route should be
Route::get('/boutique', [NameController:class,'method']);
So in your web.php file add
use App\Http\Controllers\ProductContoller
Then write your route like this:
Route::get('/boutique', [ProductContoller::class,'index']);
And I think there is a missing 'r' in your "ProductController" class name
I found out after watching a tutorial here on most common issues with new routing methodes on Laravel 8 that when uncommenting the RouteServiceProvider, using the old method require to use the old route method too on web.php, so it looks like that :
Route::get('/boutique', 'ProductController#index');
Please use
Route::get('/boutique', '\App\Http\Controllers\ProductController#index');
or use name route group and indicate the namespace
Route::group(['namespace' => 'App\Http\Controllers'], function(){
Route::get('/boutique', 'ProductController#index');
});
let me know how it works.

Calling a controller in Laravel 4

I have a controller prefixed with a namespace. How can I execute that controller? Without necessarily make a Request object on the fly. What should I do?
I have found this solution on the net:
$app = app();
$controller = $app->make(ucfirst("MyNamespace")."\\".ucfirst($controller_name)."Controller");
return $controller->callAction($controller, $parameters = array());
I get an error that says `callAction()`'s first parameter must be of type `Container`.
How should I execute a controller in Laravel from inside another controller?

Symfony2 generate controller inside custom folder

Symfony2 has a command for generating controllers
http://symfony.com/doc/current/bundles/SensioGeneratorBundle/commands/generate_controller.html
Command's default behavior is to generate the given controller inside controller folder within the bundle.
Is it possible to customize the folder where the controller will be generated ( controller/backend for example ) ?
You can try: "php bin/console make:controller Directory\\ControllerName"
You can get all the available options of this command with the help command:
php app/console help generate:controller
No you can't with the current task, but you could extend the GenerateControllerCommand to add custom options. Check out its generate function:
// GenerateControllerCommand.php
public function generate(BundleInterface $bundle, $controller, $routeFormat, $templateFormat, array $actions = array())
{
...
$controllerFile = $dir.'/Controller/'.$controller.'Controller.php';
...
$this->renderFile('controller/Controller.php.twig', $controllerFile, $parameters);
...
}

How to do simple testing of a Laravel controller?

In most web apps I've worked on, I can create a simple test script (test.php), load all dependencies (usually through an autoloader), setup the database connection, make calls to any class methods, then examine the results they return to make sure everything looks right.
Example:
$test = new Item;
var_dump($test->getItemStatus($itemid));
The above would show a nice output of the values it returns for that itemid.
With Laravel it appears to be much more complex to just perform this simple test...or maybe I am overcomplicating it and there is a simple way to do this.
Suppose I want to do the same thing in a Laravel 4 app. I want to test the output of method getItemStatus in controller named ItemsController that uses the model Items.
If I try the following, I get an Symfony \ Component \ Debug \ Exception \ FatalErrorException error:
$items = App::make('ItemsController')->getItemStatus($itemid);
If I define this route in routes.php:
Route::get('items/get-item-status', array('as' => 'getItemStatus', 'uses' => 'ItemsController#getItemStatus'));
Then try the following, I get Symfony\Component\HttpKernel\Exception\NotFoundHttpException in the request output:
$request = Request::create('items/get-item-status', 'GET', array());
$items = Route::dispatch($request)->getContent();
In my opinion in Laravel is much more simple and maybe that's why you might be overthinking it.
Let's start with a very basic router. Here are some options to do simple debug using Laravel applications in a basic router/controller:
Route::get('test', function() {
$test = new Item;
dd($test);
Log::info($test); // will show in your log
var_dump($test);
});
Now follow:
http://yourserver.com/test
And it should stop on
dd($test);
Vardumping and dying at that line. Comment it, try again, and so on.
All of these will also work in a controller:
Route::get('test/{var?}', 'TestController#index');
The very same way:
class TestController extends Controller {
public function index($var = null)
{
$test = new Item;
dd($var);
dd($test);
Log::info($var);
Log::info($test);
var_dump($test);
var_dump($var);
}
}
Your route:
Route::get('items/get-item-status', array('as' => 'getItemStatus', 'uses' => 'ItemsController#getItemStatus'));
Should also work, if you follow:
http://yourserver.com/items/get-item-status
If it doesn't, try to follow
http://yourserver.com/public/index.php/items/get-item-status
Or just
http://yourserver.com/public/items/get-item-status
Because you might have a virtual host or .htaccess configuration problem.

Categories