[英]Why route() function does now works using laravel?
In my laravel package the route does not working it is showing following incorrect route in debugger 在我的laravel软件包中,路由不起作用,它显示了调试器中以下错误的路由
Request URL: http://localhost:8000/%7B%7B%20route('contact')%20%7D%7D
However my route is as following 但是我的路线如下
Route::group(['namespace' => 'ayazdev\Contact\Http\Controllers'], function(){
Route::get('contact', 'ContactController@index')->name('contact');
Route::post('contact', 'ContactController@send')->name('sendForm');
});
And following is where I am calling the route 接下来就是我所说的路线
$(function(){
$("#contact-form").submit(function(e) {
var form = $(this);
$.ajax({
type: "POST",
url: "{{ route('contact') }}",
data: form.serialize(),
success: function(data)
{
alert(data);
}
});
e.preventDefault();
});
});
if above detail does not enough to understand then you can kindly check it on github . 如果以上细节不足以理解,则可以在github上进行检查。
Can someone kindly guide me why it is now working, I will appreciate. 有人能指导我现在为什么会工作,我将不胜感激。 Thanks
谢谢
The curly braces are part of the Laravel Blade views, but you are using this in a JavaScript file . 花括号是Laravel Blade视图的一部分,但是您正在JavaScript文件中使用它。 This code is not parsed by Laravel, so you cannot use php functions here.
Laravel不会解析此代码,因此您不能在此处使用php函数。
If you want to get named routes in your JavaScript code, you will have to render them into a JavaScript variable or use a package like Ziggy to get route functionality in JavaScript. 如果要在JavaScript代码中获取命名路由,则必须将其渲染为JavaScript变量,或使用Ziggy之类的包来获取JavaScript中的路由功能。
As noted by Jerodev the curly braces are from Laravel Blade and you are probably using it in a Javascript file. 如Jerodev所述,花括号来自Laravel Blade,您可能在Javascript文件中使用了它。 Either you can move it to a blade file as such:
您可以将其移动到刀片文件中,如下所示:
<script>
$(function(){
$("#contact-form").submit(function(e) {
var form = $(this);
$.ajax({
type: "POST",
url: "{{ route('contact') }}",
data: form.serialize(),
success: function(data)
{
alert(data);
}
});
e.preventDefault();
});
});
</script>
Or if you prefer to keep it in a separate file you can have a tag with just the information about that route and get it via jQuery as you are doing: 或者,如果您希望将其保存在单独的文件中,则可以使用仅包含有关该路线的信息的标记,并在执行操作时通过jQuery获取它:
// at the blade file add
<div id="routeToContact" data-route="{{ route('contact') }}">
// At the javascript file you can do the following
var route = $("#routeToContact").data('route');
$("#contact-form").submit(function(e) {
var form = $(this);
$.ajax({
type: "POST",
url: route,
data: form.serialize(),
success: function(data)
{
alert(data);
}
});
e.preventDefault();
});
As a personal taste I would chose the second method to keep everything organized but as my mom always said: "choose what your heart beats for" 根据个人喜好,我会选择第二种方法来使一切保持井井有条,但正如妈妈一直说的那样:“选择您的心脏跳动的目的”
You are using a Blade syntax in a simple Javascript file. 您在一个简单的Javascript文件中使用Blade语法。
Try to do the following: 尝试执行以下操作:
$(function(){
$("#contact-form").submit(function(e) {
var form = $(this);
$.ajax({
type: "POST",
url: "<?= route('contact'); ?>",
data: form.serialize(),
success: function(data)
{
alert(data);
}
});
e.preventDefault();
});
});
That way, you use PHP itself to get the address of the desired route. 这样,您就可以使用PHP本身来获取所需路由的地址。
See more about this sintax in: https://secure.php.net/manual/pt_BR/ini.core.php#ini.short-open-tag 在以下网址中查看有关此正税的更多信息: https : //secure.php.net/manual/pt_BR/ini.core.php#ini.short-open-tag
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.