I have a laravel application which is deployed on my server.
File structure
server
│
└───blog-app
│ │ app
│ │ bootstrap
│ │ ......
│
└───public_html
│ blog
│ │─── index.php
│
This is what my structure looks like. Up until now everything seemed to work fine, however now I was trying to make an AJAX call to my controller function but it doesn't work as it should have.
Let me explain it by an example,
$.ajax({
type:'POST',
url:'{{route('blog.prefetchResults')}}',
data:'_token = <?php echo csrf_token() ?>',
success:function(data){
$("#msg").html(data.msg);
}
});
This is my ajax call which goes to the route blog.prefetchResults
which points it to a controller that resides in the BlogController
. However when I look in the browser console I see this kind of error:
http://mysubdomain.com/blog/prefetchBlogController@prefetchResults 500 (Internal Server Error)
What I can infer from this error is that it does not allow me to access those directories when a client side request is made, this makes me ask a very simple question: How am I supposed to make an AJAX request using laravel if I can't do it this way?
Update(EXample):
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
test.blade.php
@extends('main')
<p>
This is an ajxax test
</p>
<button class="butt">
click me
</button>
<script>
$().ready(function(){
$('butt').on('click',function(){
$.ajax({
type:'GET',
url:'{{route('blog.test')}}',
success:function(data){
$("p").html("somthing");
}
});
})
});
</script>
Web.php
//Test controller
Route::get('blog/test'.'BlogController@test')->name('blog.test');
BlogController
public function test(){
return view('test.test')->withMessage('changed');
}
File structure
File structure
server
│
└───blog-app //Not accessible through client side languages
│ │ app->Http->Controllers/BlogController.php
│ │ resources->views->test->test.blade.php
│ │ routes->web.php->route defined here
│
└───public_html(www folder)// accessible to the client (www.mydomain.com)
│ blog
│ │─── index.php
│
@Alejandro Reply
- corrected the above problems.
- Sorry about
app->Http->BlogController.php
,it'sapp->Http->Controllers/BlogController.php
.
Note:
The public_html directory is the root directory, that is the root point for my
mydomain.com
.So the other folders such as
blog-app
is not accessible for the normal users,they can be only accessed by server side languages.
Log File
[2017-01-08 19:23:01] local.ERROR: exception 'ErrorException' with message
'Trying to get property of non-object' in
/home/mydomainuser/blogapp/storage/framework/views/af6b0045a9d30a80c19dd552032d46b39ad1cc99.php:1
Next exception 'ErrorException' with message
'Trying to get property of non-object (View: /home/foodq7y4/blogapp/resources/views/blog/single.blade.php)' in
/home/mydomainuser/blogapp/storage/framework/views/af6b0045a9d30a80c19dd552032d46b39ad1cc99.php:1
.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
Single.blade.php
@extends('main')
@section('title', '| Blog')
@section('content')
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>Blog</h1>
</div>
</div>
@foreach ($posts as $post)
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h2>{{ ucwords($post->title) }}</h2>
<h5>Published: {{ date('M j, Y', strtotime($post->created_at)) }}</h5>
<p>{{ substr(strip_tags($post->body), 0, 250) }}{{ strlen(strip_tags($post->body)) > 250 ? '...' : "" }}</p>
<a href="{{ route('blog.single', $post->slug) }}" class="btn btn-sm btn-primary">Read More</a>
<hr>
</div>
</div>
@endforeach
<div class="row">
<div class="col-md-12">
<div class="text-center">
{!! $posts->links() !!}
</div>
</div>
</div>
@endsection