命令行创建控制器
php artisan make:controller UserController
将控制器放到其他文件夹后的解决办法:
1.创建一个 CommonController.php ,其他文件都继承他
<?php namespace App\Http\Controllers\Admin; use Illuminate\Http\Request; use App\Http\Controllers\Controller; class CommonController extends Controller { /* //图片上传 public function upload() { $file = Input::file('Filedata'); if($file -> isValid()){ $entension = $file -> getClientOriginalExtension(); //上传文件的后缀. $newName = date('YmdHis').mt_rand(100,999).'.'.$entension; $path = $file -> move(base_path().'/uploads',$newName); $filepath = 'uploads/'.$newName; return $filepath; } }*/ }
调用的文件书写如下:
<?php namespace App\Http\Controllers\Admin; use App\Http\Model\Login\Admin; use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\Redis; require_once 'org/code/Code.class.php'; class AdminLoginController extends CommonController { public function login()
2.直接修改命名空间 和引用
<?php namespace App\Http\Controllers\BettingServer; //当前文件的命名空间BettingServer为所在目录 use Illuminate\Http\Request; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Input; require_once 'Classes/KenoBettingCheck.php'; class kenoBettingController extends Controller
控制器创建show方法
<?php namespace App\Http\Controllers; use App\User; use Illuminate\Http\Request; class UserController extends Controller { /** * 为指定用户显示详情 * * @param int $id * @return Response * @author LaravelAcademy.org */ public function show($id) { return view('user.profile', ['user' => User::findOrFail($id)]); } }
我们可以像这样定义指向该控制器动作的路由:这里注意参数的传递
Route::get('user/{id}', 'UserController@show');