安装好php环境与laravel框架后,然后就可以使用了。
一、 先看下路由的使用routes.
找到app/Http/routes.php文件后,设置如下
1.
Route::get('/', function () { return 'Hello World'; });
然后访问localhost:8000/ 就会出现 Hello World
2. 可以这样设置
Route::get('post', ['uses' => 'PostController@index', 'as' => 'name']);
前提是要先生成一个postcontroller文件。
//使用Artisan命令make:controller
php artisan make:controller PostController
可以在index()方法中输出数据
public function index(){ echo 'hello index';exit; }
访问localhost:8000/post则会出现文字 hello index
扫描二维码关注公众号,回复:
327098 查看本文章
二、再来看看链接mysql的使用
1. postController.php文件
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB; //使用数据库DB
use App\Http\Requests;
use App\Http\Controllers\Controller;
class PostController extends Controller
{
public function show($id,$name='')
{
$users = DB::select('select * from users where id = ?',[$id]);
foreach ($users as $val){
echo $val->name; //以对象的形式返回数据
}
exit;
}
}
访问url:http://localhost:8000/post/show/3 则会输出数据test003
那么,如何链接的数据库呢。这里需要看database文件,在config/database.php
如果出现:
Laravel 5 error SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)
的报错。需要修改配置文件.env,默认该文件是隐藏的,打开隐藏文件,然后编辑改文件的
DB_HOST=localhost
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=
这段代码与database文件一直就好了。