本篇文章给大家带来了关于laravel的相关知识,其中主要介绍了关于路由和控制器的相关问题,包括了路由组、跳到控制器、post路由、ajax路由等等相关内容,下面一起来看一下,希望对大家有帮助。
【相关推荐:laravel视频教程】
laravel访问路径是:
1 ) 路由—控制器—页面/输出
2 ) 路由—匿名函数—页面/输出
一、查看当前所有路由进入当前项目的根目录之后运行cmd
或者用ide自带的终端terminal,快捷键 alt+f12
php artisan route:list
二、各种路由在routes/web.php文件
我域名是www.la.com,按自己实际情况来
1.跳到视图route::get('/', function () { return view('welcome');});
视图目录位置:resources/views,存放的也是 html 内容。view()是一个助手函数,view(‘welcome’) 表示跳到welcome.blade.php视图,也就是我们第一次启动 laravel 看到的那个欢迎页面。
在浏览器地址栏写:www.la.com/ ,运行结果为:
2.直接输出route::get('ok', function () { echo hello world;});
3.带参数的的路由dump()是laravel的辅助函数,用来打印数据的
1)单个参数route::get('show/{a}', function ($a) { dump($a);});
浏览器运行http://www.la.com/show/1
结果:“1”
注意:是字符串
2)多个参数route::get('show/{a}/{b}', function ($a,$b) { echo $a.','.$b;});
浏览器运行:http://www.la.com/show/1/hello
结果:1,hello
4.路由参数添加限定 正则表达式route::get('user/{name}/{age}', function ($name,$age) { echo $name.' '.$age; //直接输出 })->where('age','\d+')->where('name','[a-za-z]+');
上述限定的意思是 age 参数只能接受数字,name 参数只能接受大小写字母。
如果不满足条件,结果:404 not found
浏览器中运行:http://www.la.com/user/zhangsan/18
结果:zhangshan 18
5.路由组1)第一种写法route::group(array(‘prefix’=>‘user’),function(){});route::group(array('prefix'=>'user'),function(){ route::get('/index', function () { echo 'index'; }); route::get('/add', function () { echo 'add'; });});
浏览器运行:
http://www.la.com/user/indexhttp://www.la.com/user/add结果:
indexadd2)第二种写法 route::prefix(‘user’)->group(function(){});route::prefix('user')->group(function(){ route::get('/index', function () { echo 'index'; }); route::get('/add', function () { echo 'add'; });});
6.跳到控制器1)创建控制器,编写方法在项目根目录运行
php artisan make:controller testcontroller
<?phpnamespace app\http\controllers;use illuminate\http\request;class testcontroller extends controller{ public function hello(){ echo "testcontroller的hello方法"; }}
2)写路由在config/web.php最开始添加
use app\http\controllers\testcontroller;
然后写路由
route::get('/hello',[testcontroller::class,'hello']);//跳到控制器的方法
浏览器运行:http://www.la.com/hello
结果:
7.post路由laravel中为了防止csrf攻击,我们在每一个post表单里面都要写上一句 @csrf ,详细可以点击看我另一篇文章
我们先在views/user文件夹添加一个add.blade.php视图里面代码:
<!doctype html><html><head> <title>测试post提交</title></head><body> <form method="post" action="/user/insert"> @csrf name:<input type="text" name="name"> <input type="submit" value="提交" /> </form></body></html>
添加路由use illuminate\http\request;route::prefix('user')->group(function(){ route::get('/add', function () { return view('user.add'); }); route::post('/insert', function (request $request) { dump($request->all()); echo post路由验证成功; });});
view('user.add')的意思是在resources/views目录下的user文件夹下的add视图 。(resources/views是默认路径)
$request->all()获取所有请求参数
dump() 打印数据
测试
首先直接输入http://www.la.com/user/insert肯定是不行的,会报错(the get method is not supported for this route. supported methods: post.)。
postman 输入http://www.la.com/user/insert post提交失败 返419 | page expired
所以我们先浏览器输入http://www.la.com/user/add ,name随便填啥点提交
8.ajax路由头部要加入
通过js,传递 token,这里 name=_token 随便取什么名
headers: {
‘x-csrf-token’: $(‘meta[name=_token]’).attr(‘content’)
},
<!doctype html><html><head> <meta charset="utf-8"> <title>csrf</title> <meta name="_token" content="{{csrf_token()}}"></head><body><script src="/jquery-3.6.0.min.js"></script><script> $.ajax({ url: http://www.la.com/index,//本页面 type: post, data: { name:名字 }, headers: { 'x-csrf-token': $('meta[name=_token]').attr('content') }, success: function (data) { console.log(200); } });</script></body></html>
9.带别名的路由别名路由就是给某一个路由起一个别名,直接使用使用原名可以访问路由,但直接使用别名不能访问这个路由,同时在其他页面调用别名可以访问这个路由。
route::get('user/profile',function(){ return 'my url:'.route('profile');})->name('profile'); //创建一个路由 user/profile,这个路由的作用是返回路由 profile 的 rul 地址,并给这个路由起一个别名 profile route::get('redirect',function(){ return redirect()->route('profile'); }); //创建一个名为 redirect 的路由,这个路由的作用是跳转到路由 profile。
route() 生成完整的url
redirect()->route(‘profile’); //重定向命名路由
在浏览器中运行 www.la.com/user/profile
结果:
在浏览器中运行www.la.com/profile
结果:404 not found
在浏览器中运行www.la.com/redirect
结果:
10.命名空间路由之前写的控制器 controller 都直接写在 http\controllers 文件夹之中,但实际情况是控制器也会分类,比如与管理员相关的操作会在 controllers 中,再建一个文件夹 admin,然 后把所有关于管理员的控制器类都放在这个文件夹中。如果这样的话,就要添加名称空间。
创建控制器
方法一:使用phpartisanphp artisan make:controller admin\indexcontroller
使用这种方法创建的控制器,自动加载名称空间,如下图所示
观察与之前创建控制器php artisan make:controller testcontroller的区别
方法二:复制粘贴其他类
在controllers文件夹下创建admin文件夹,复制之前创建的控制器testcontroller,照着上图修改。
命名空间 namespace app\http\controllers\admin;
添加类引用 use app\http\controllers\controller;
控制器添加 index方法public function index(){ return admin文件夹下的indexcontroller中的index方法;}
写路由
web.php文件use app\http\controllers\admin\indexcontroller;route::group(['namespace'=>'admin'],function(){ route::get('admin',[indexcontroller::class,'index']);//管理员的主页 route::get('admin/user',[indexcontroller::class,'index']);//管理员用户相关 route::get('admin/goods',[indexcontroller::class,'index']);//商品相关});
浏览器输地址
http://www.la.com/admin
http://www.la.com/admin/user
http://www.la.com/admin/goods
结果都是一样
【相关推荐:laravel视频教程】
以上就是一起来聊聊laravel8的路由与控制器的详细内容。