I am calling this function:
Route::get('/person', function (){
$person=[
'first_name'=>'dildar',
'last_name'=>'Muhammad',
];
return $person;
});
and I browse https://astrolabe.ml/api/person but I got error not found page 404
it does work on new laravel project but I did not found any clue from that why it does not my work.
Update your code
Route::get('/person', function (){
$person=[
'first_name'=>'dildar',
'last_name'=>'Muhammad',
];
return response()->json($person); // Update this thing
});
emphasized texttry to replace this
Route::get('/person', function (){
to
Route::get('person', function (){
Edit:
remove everything and try this:
Route::get('test', 'HomeController#index');
HomeController.php
public function index(){
dd('x');
}
then run:
php artisan optimize
finally go to www.example.com/api/test
Related
I made a manyToMany relationship and want to return that in my php code which is not working, but when I run the same code in tinker it is working for some reason. What am I missing?
// Firma
public function auftraege()
{
return $this->belongsToMany("Auftrag", 'auftraege_firma');
}
// Auftrag
public function firmen()
{
return $this->belongsToMany("Firma", 'auftraege_firma');
}
// works in tinker
$firma = App\Firma::first();
$firma->auftraege
// Does not work in php Controller
$firma = App\Firma::first();
return $firma->auftraege
Getting 500 Error
Looking at your controller code, I can only notice two things. Change your controller code like this:
$firma = \App\Firma::first();
return $firma->auftraege;
You are missing \ before the App namespace and also the semicolon is missing in the return statement.
Please also change the relationshps like this:
public function auftraege()
{
return $this->belongsToMany(Auftrag::class, 'auftraege_firma');
}
public function firmen()
{
return $this->belongsToMany(Firma::class, 'auftraege_firma');
}
The reason it was working from tinker is that by default tinker sets the namespace to App for the current tinker session. That's why even though you didn't specify the App namespace, tinker was able to parse the proper namespace.
I have a few lines of code which is written in express js, I want to convert into the laravel code,
app.get('/*', function(req, res) {
var jsonResponse = [];
jsonResponse.push({ "text": "Hi. 3 is a lucky number..." });
res.send(jsonResponse);
});
Is there anyway? Please guide me.
Here is my tried , don't know correct or not.
public function json_test(){
$message =["text" => "Hi. is a lucky number..."];
return $message;
}
Long story short
I think everything is correct. You just have to change your return value to be a json response.
A bit more detailed
You must first define a route in api.php:
Route::get('/some/url', 'ExampleController#jsonTest');
Next you must define a controller and inside the controller the functions that you need:
<?php
namespace App\Http\Controllers;
class ExampleController extends Controller
{
public function jsonTest(){
$message =["text" => "Hi. is a lucky number..."];
return response()->json($message);
}
}
In case you don't need an entire controller for this you can simply place the functionality in the api.php file like so:
Route::get('/some/url', function () {
$message =["text" => "Hi. is a lucky number..."];
return response()->json($message);
});
Hope this helps.
I'm new in Laravel and I'm trying to create a View in Acelle (app based on Laravel). I read a lot of tutorials, but I've not been luck with this problem.
I created the view "lol.blade.php" on /resources/views folder with this code:
HELLO (just hello)
The Route:
Route::get('lol', function()
{
if (view()->exists('lol')) {
//return 'helloooo'; <--- it works
return view('lol');
} else {
return 'not exists';
}
});
The code knows the view exists, but the url (localhost/acelle/public/lol) prints this message:
"Whoops, looks like something went wrong."
I can't solve the problem with tutorials. I followed all the steps about creating views in Laravel, but I don't know why the view prints that message.
Please help!
PS: Laravel version: 5.2.45
EDIT:
In console [network] shows Error 500. and laravel.log prints 59 lines. but the first line show:
[2017-07-14 14:08:20] production.ERROR: ErrorException: Undefined index:controller in /home/acelle/public_html/acelle/app/Providers/AppServiceProv‌​ider.php:20
You posted this in the comments:
app('view')->composer('*', function ($view) {
$action = app('request')->route()->getAction();
$controller = class_basename($action['controller']);
list($controller, $action) = explode('#', $controller);
$view->with(compact('controller', 'action'));
});
Your issue is that this route uses a closure, and has no controller:
Route::get('lol', function() {});
Therefore, $action['controller'] doesn't exist and throws a warning as a result. You'll want to check isset($action['controller']) before doing the rest of your code that uses the controller variable.
Already solved!!
SOLUTION:
creating a controller : MiwebController.php
<?
namespace Acelle\Http\Controllers;
class MiwebController extends Controller
{
public function __construct()
{
parent::__construct();
$this->middleware('auth');
}
public function index()
{
return view('lol');
}
}
?>
routes.php:
Route::get('lol', 'MiwebController#index');
It works fine. Thank you!
I am making my own package.
I simplified the code to explain the weird error.
I have a simple method inside a simple controller. First, i am sending an email (printing in laravel.log) and works fine.
public function signup(){
Mail::send('vendor.proto.register.emails.proto-register-new-account', [], function ($m) {
$m->from('noreply#test.com', 'Hello');
$m->to('sfs#dgf.c', 'pepe')->subject('Test. Congratulations, your account was created');
});
return view('view path');
}
But when i want to move the Mail:: to a private method like this.
public function signup(){
$this->sendNotification2();
return view('view path');
}
public function sendNotification2(){
Mail::send('vendor.proto.register.emails.proto-register-new-account', [], function ($m) {
$m->from('noreply#test.com', 'Hello');
$m->to('sfs#dgf.c', 'pepe')->subject('Test. Congratulations, your account was created');
});
}
It crash an print the error
FatalErrorException in ClassLoader.php line 373: Maximum function nesting level of '100' reached, aborting!
Is it a bug? or i am doing something wrong ?
Solution.
Googling i found this solution. Notice i am using laravel 5.2 and php 5.6.7
Add in bootstrap\autoload.php this code ini_set('xdebug.max_nesting_level', 200); and it fix the problem.
I just can't retrieve the data in my query string section.
I've used AJAX request throughout my website to implement a wide variety of tasks asynchronously and didn't had an issue of this kind.
Route
Route::get('/mensagem/enviar_mensagem', [ 'as' => 'mensagem.enviar_mensagem', 'uses' => 'MensagemController#enviar_mensagem']);
the testing url:
http://mydomain.com.br/mensagem/enviar_mensagem?para=email#bol.com.br
my action method:
public function enviar_mensagem(Request $request)
{
$para = $request->get('para');
//$para = $_GET['para']; I get an undefined index error
echo $para; //always empty string!
}
You need to use input. Like so:
Also, for testing, return versus echo.
public function enviar_mensagem(Request $request)
{
$para = $request->input('para');
return $para;
}
And to spark my curiosity, what does return $request->all() return?
Well, the provided code seems to be correct. Make sure you use \Illuminate\Http\Request. This code
Route::get('/mensagem/enviar_mensagem', function(\Illuminate\Http\Request $request) {
return $request->para;
// return $request->get('para'); // also works
});
returns email#bol.com.br by request http://your-site.app/mensagem/enviar_mensagem?para=email#bol.com.br
I copy pasted your code and both works:
$para = $request->get('para');
$para = $_GET['para'];
//$para = $_GET['para']; I get an undefined index error
Did you make sure the webserver is properly handling the HTTP request?
https://laravel.com/docs/5.4#web-server-configuration
You can try with below code :
use Request;
class xyzController {
public function enviar_mensagem()
{
$para = Request::all();
echo $para['para'];
}
}
First you will need to change the route to add also this
Route::get('/mensagem/enviar_mensagem/{para}',
[ 'as' => 'mensagem.enviar_mensagem', 'uses' =>
'MensagemController#enviar_mensagem']);
And after that in controller
public function enviar_mensagem($para){
return var_dump($para);
}
Use the route method on the request object to access GET parameters
public function enviar_mensagem(Request $request)
{
$para = $request->route('para');
echo $para;
}