简体   繁体   English

为什么使用laravel现在可以使用route()函数?

[英]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.

相关问题 在这种情况下,为什么 route() 在 Laravel 上有效? - Why does route() works in view in this case on Laravel? 在Laravel中,为什么在应用程序事件之前,Request :: segment()方法可以正常工作,而Route :: currentRouteName()却不能正常工作? - In Laravel, Why with the `before` application event, `Request::segment()` method works fine and `Route::currentRouteName()` does not? Laravel路由到控制器中的自定义功能不起作用 - Laravel route to custom function in controller does not work Javascript function 不向 Laravel 路由发送数据 - Javascript function does not send data into Laravel route laravel4路由可与路由/一起使用,但不适用于其他路由 - laravel4 routes works with route /, but does not for other routes 名称空间如何在laravel路由组内部工作 - How does namespace works inside of laravel route group 为什么 carbon::now() 不适用于观察者或主机(laravel)中的工作? - why carbon::now() does not work on observer or job in host (laravel)? 用于添加数据透视表的 Laravel 路由在模型中起作用,而控制器没有 - Laravel route to add in pivot table works function in model with controller not 路由 [] 未定义。 使用 Route::currentRouteName() 函数在 Laravel 6.2 中出错 - Route [] not defined. error in laravel 6.2 using Route::currentRouteName() function Laravel 5路线仅适用于斜线 - Laravel 5 route only works with slash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM