Laravel : artisan command not generating complete auto generated code - php

I started to learn through a tutorial for learning Laravel 5.
https://tutorials.kode-blog.com/laravel-hello-world
I followed the steps
when I run the command : php artisan make:controller Hello
I am getting just the base code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class Hello extends Controller
{
//
}
However the tutorial specify the auto-generated code with some functions as well inside the class.
It says the auto-generated code is,
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class Hello extends Controller
{
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param Request $request
* #return Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param Request $request
* #param int $id
* #return Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return Response
*/
public function destroy($id)
{
//
}
}
I could not figure out what configuration changes need to be updated or I am missing something very basic for this framework to start with, I tried to reinstall the application again and the same issue happened again.

That tutorial looks a little dated. In Laravel 5.2, the command was updated to generate a plain controller by default.
To generate a "resource" controller, as shown in the tutorial, you now need to pass the "--resource" flag:
php artisan make:controller Hello --resource

the Artisan command you are looking for is:
php artisan make:controller UserController --resource
You could find all information about Controllers in the official documentation:
Laravel Controller documentation
Or Google for information but specify your Laravel version

Related

How do i make a Laravel web.php route work on a new project?

sorry for my english.
I'm trying to create a laravel route but i just can't make it work.
My project name is "portalRAG". It's a web app. When i access "my.address/PortalRAG"
it works just fine, but i can't make any other route work.
This is a new Laravel Project. It's almost empty and i haven't touched any major configuration other than creating some 1 or 2 views,controllers and model and only created some html code.
Here's my web.php file:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers;
use App\Http\Controllers\ragController\ragHomeController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('login');
});
/* NOT WORKING
Route::get('test', function () {
return view('login');
});
*/
Route::get('test','App\Http\Controllers\ragController\ragHomeController')->name('test');
I simply want to access "test" route. The controller i'm trying to use it's called ragHomeController and it's inside a ragController (a folder inside the basic Controller file).
Here's ragHomeController.
<?php
namespace App\Http\Controllers\ragController;
use App\Http\Controllers\Controller;
use App\Models\ragModel\ragHomeModel;
use Illuminate\Http\Request;
class ragHomeController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
echo("WHATEVER");
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param \App\Models\ragModel\ragHomeModel $ragHomeModel
* #return \Illuminate\Http\Response
*/
public function show(ragHomeModel $ragHomeModel)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Models\ragModel\ragHomeModel $ragHomeModel
* #return \Illuminate\Http\Response
*/
public function edit(ragHomeModel $ragHomeModel)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Models\ragModel\ragHomeModel $ragHomeModel
* #return \Illuminate\Http\Response
*/
public function update(Request $request, ragHomeModel $ragHomeModel)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param \App\Models\ragModel\ragHomeModel $ragHomeModel
* #return \Illuminate\Http\Response
*/
public function destroy(ragHomeModel $ragHomeModel)
{
//
}
public function __invoke()
{
}
}
What i'm getting wront? i have tried clearing cache, clearing route cache, and nothing works.
How should i access my "test" route? (I have tried every way and i still can't make it work).
"my.address/PortalRAG/test"?
"my.address/test"?
please write you route like below
Route::get('test',[ragHomeController::class,'Your_Method_Name'])->name('test');
and import your controller in top of your web
use App\Http\Controllers\ragHomeController;
Note:sometime it won't work so you have to run
php artisan optimize
if you are using xampp to run your laravel project then you can access your route like below
"my.address/PortalRAG/public/test"
you didn't use the method that located in ragHomeController so write it in this way
Route::get('test',[App\Http\Controllers\ragController\ragHomeController::class, 'index'])->name('test');
now open your route like this:
my.address/PortalRAG/test

Why is laravel not generating detailed code?

I am following a tutorial and from what I'm reading php artisan controller:make OrdersController gives
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class Hello extends Controller
{
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param Request $request
* #return Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param Request $request
* #param int $id
* #return Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return Response
*/
public function destroy($id)
{
//
}}
However in my case I'm only getting:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class Hello extends Controller
{
//
}
I don't get the included methods and my boiler plate is clearly not as detailed. I'm using the same command php artisan make:controller Hello. What could I be doing wrong? If it helps I'm on OSX.
The first class is a resource controller, read the docs
If you want an explenation regarding an artisan command add help before the command:
php artisan help make:cotroller
you can see the options (-r for resource) to make a resource controller.
You can manually delete the generated controller and rerun the appropriate command:
php artisan make:controller Hello -r
Don't forget to add the route in your resources/web.php file

Laravel Auto Routes Like Codeigniter - Custom Solution

When i didn't find any best solution for auto route so i write my code.
Any suggestion will be appreciate.
In your routes.php file write this line at the end of the file
Route::match(["get","post"], '/{controller}/{method?}/{parameter?}', "Routes#index");
Now Create a new class in App\HTTP\Controllers
Routes.php (you can change name)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class Routes extends Controller
{
public function index($controller,$method="",$parmeter=""){
$controller = ucfirst($controller);
if(empty($method)){
// e.g: example.com/users
// will hit App\Http\Controllers\Users\Users.php Class Index method
return \App::call("App\Http\Controllers\\$controller\\$controller#index");
}
// e.g: example.com/users/list
// will hit App\Http\Controllers\Users\Users.php Class List method
// If user.php has List method then $parameters will pass
$app = \App("App\Http\Controllers\\$controller\\$controller");
if(method_exists($app, $method)){
return $app->$method($parmeter);
}
// If you have a folder User and have multiple class in users folder, and want to access other class
// e.g: example.com/users/groups
// will hit App\Http\Controllers\Users\Groups.php Class Index method
$method = ucfirst($method); //Now method will be use as Class name
$app = \App("App\Http\Controllers\\$controller\\$method");
return $app->index();
}
}
DONE
Now create your Classes in Controllers Folder and it will auto route...
Your file structure E.g:
App
HTTP
Controllers
Users
Users.php
Groups.php
Etc.php
Post
Post.php
Banners
Banners.php
Folder
File.php
Now you have idea, You can change logic according to your style, or you can use this it will work.
I am using Laravel 5.2
As per my understanding, this could be the solution for you.
Laravel has these built in methods for its controller
<?php
class TestsController extends BaseController {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function store()
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param int $id
* #return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return Response
*/
public function destroy($id)
{
//
}
}
Route can be defined using following syntax :
<?php
Route::resource('tests', 'TestsController');
These actions will be handled:
GET /tests tests.index
GET /tests/create tests.create
POST /tests tests.store
GET /tests/{id} tests.show
GET /tests/{id}/edit tests.edit
PUT/PATCH /tests/{id} tests.update
DELETE /tests/{id} tests.destroy

Laravel route model binding does not get ID

I'm learning Laravel 5 and found something that I do not understand. If I put this code to route, than everything goes fine:
Route::bind('addresses', function($value, $route) {
return App\Address::find($value)->first();
});
But without it I got the:
Whoops, looks like something went wrong. 1/1 ReflectionException in
Container.php line 736: Class Address does not exist
error.
My routes:
Route::model('addresses', 'Address');
Route::resource('addresses', 'AddressesController');
My AddressesController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Address;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class AddressesController extends Controller
{
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$addresses = Address::all();
return view('addresses.index', compact('addresses'));
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param Request $request
* #return Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param Address $address
* #return Response
*/
public function show(Address $address)
{
return view('addresses.show', compact('address'));
}
My question is why I get the error code if I do not bind the ID value directly in my route. Laravel says with RESTful Resource Controller I got the addresses/{address} with ID automatically, but it seems not. Any idea? Thanky for your help.
Route::model takes full class name. Route::model('addresses', 'App\Address');

laravel 4 restful controller wont work

I am trying to make the laravel 4 controller in mac terminal by
php artisan controller:make UserController
It work's and insert the controller in the folder.
In my route.php i add:
Route::controller('users', 'UserController');
In my UserController in index i make
return "Hello world"
But when i am entering localhost/users it don't show anything, either in /users/create.
What can i do?
Trace errors:
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
open: /Applications/XAMPP/xamppfiles/htdocs/salety/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php
* #param Exception $e
* #return void
*/
protected function handleRoutingException(\Exception $e)
{
if ($e instanceof ResourceNotFoundException)
{
throw new NotFoundHttpException($e->getMessage());
}
UserController
<?php
class UserController extends \BaseController {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
return "Hello world!";
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function store()
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param int $id
* #return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return Response
*/
public function destroy($id)
{
//
}
}
You need to change Index to getIndex when using RESTful controllers.
What you've created using the artisan command is a resource controller.
To get this to work, change your routes.php file to this:
Route::resource('users', 'UserController');
This will make the /users route a resource and allow it to respond properly.
Be sure to look at the documentation on resource controllers and be sure to pay attention to the Actions Handled By Resource Controller section, as this gives you the key to what methods are used for which URI's.
Well for restfull controllers you need to use this form getIndex , getCreate , postRegister..etc , you can either use Route::controller() or Route::resource()
after changing stuff in your routes.php you need to run
php composer dump-autoload
to refresh the autoloading files with edited routes.

Categories