You mean instead of using Laravel? Just write your own framework?
It's giving me NULL value.
You have to pass the name in route or url like I showed above:
somesite.com/john
Passing parameters is in https://laravel.com/docs/8.x/routing
:-( :-( :-(
Because I am not getting way to do that!!!
Thank you for your help.
Let me think in another way!
Finally it's not possible to pass data in this way!
Perhaps if you explained the scenario rather than just keep saying to you want to pass FIXED string to controller and everyone else saying declare the fixed string in the controller.
What problem are you trying to resolve?
@mrperfectionist as @snapey and I have said. Describe what you are trying to build, not how you are trying to do it.
I think it was @jlrdw that posted this, but I think its relevant.
Trying to solve a problem which will accept one Request and output one file.
With that Request I want call a Class Function which will accept one/two parameters and give me file output with that name.
This is the problem.
Now, with a Request how will I call a Class which will accept parameter and giving me a file output.
This is my problem which trying to solve.
So, I am trying to Call a Controller Class Function with my parameter from Route.
You are not using a view, is that correct?
Yes.
I am not using any view
It's an odd ask but why not ?
You can get it with a route middleware :
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class SetName
{
public function handle(Request $request, Closure $next)
{
$request->name = 'Mr. Perfectionist';
return $next($request);
}
}
Add it in Kernel :
protected $routeMiddleware = [
...
'name' => \App\Http\Middleware\SetName::class,
];
Add middleware in your route :
use App\Http\Controllers\DateController;
Route::get('/', [ DateController::class, 'holiday' ])->middleware('name');
Then in your controller :
class DateController extends Controller
{
public function holiday(Request $request) {
return $request->name;
}
}
What was wrong with the example I gave? The route calls a function that has the FIXED parameter which then calls a second function giving it that parameter
@bestmomo Same as
public function holiday()
{
return 'Mr. Perfecionist';
}
@mrperfectionist I would advise you to watch https://laracasts.com/series/laravel-6-from-scratch
You will learn the basics there.
@bestmomo i admire your solution. making something so simple so complicated :P
He wants variable at route level, I really don't understand why ^^
I still think the "from scratch series" is the correct answer.
is this the answer?
@mrperfectionist So are you going to use what @bestmomo gave you?
@edoc No it's really bad answer. It's just an unnecessary code.
I am not going to use it.
@jlrdw gave me correct Answer. But I am not so much expert to start with Laravel Scratch. So, I started with PHP Practitioner and OOP PHP Course of Laracast. Then I will dive on that.
By the way, Still finding the answer......................... Want to call a Class onload Laravel App.
Hello Seniors,
How about my Solution?
Is it okay? It's giving me my desired Result.
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Route;
Route::get('/', function(){
$data = "Name";
return App::call('App\Http\Controllers\DateController@holiday' , ['data' => $data]);
});
DateController:
public function holiday($data) {
echo $data;
}
It's same as
Route::get('/', function() {
echo "Name";
});
no, its not ok
Its the same as
Route::get('/', function(){
return (new App\Http\Controllers\DateController)->holiday('Name');
});
which is the same as
Route::get('/', [App\Http\Controllers\DateController::class,'holiday']);
and with controller method
public function holiday($name = 'Name')
or with controller method
public function holiday()
{
$name = 'Name';
Okay. But what about the Passing data to Controller? From my solution I can pass parameter as much as I want.
You can only pass what you have hard coded into the route file. This is no different to hard coding it in the controller itself.
Still no clue what you are trying to do.
I've been following this thread for a while and come to think that maybe @mrperfectionist might be after explicit route model binding?
https://laravel.com/docs/8.x/routing#explicit-binding
Also agree that, from the given remarks, it doesn't seem different than hardcoding a value into the controller itself.
@rodrigo.pedra having also followed this thread, the problem is that we still don’t know why @mrperfectionist is trying to do what he’s trying to solve.
He asked the equivalent of ‘how can I stab this screw driver into the plug socket’. Finding the why means we can advise to use the switch instead of watching him electrocute himself.
I can give you a use case for his example:
When Route::domain doesn't do the trick...
Route::get('/', function () {
$url = parse_url(URL::full());
$domain = explode('.', $url['host']);
$subdomain = $domain[0];
...then, pass the $subdomain to the controller
route.php
Route::group(['name' => 'hello world'], function () {
Route::get('/', [DateController::class, 'holiday']);
});
DateController.php
holiday ($request) {
echo $request->route()->action['name'];
}
I onced in this situation. Although the situaion, use case is pretty rare, I coudn't say this is completely useless For example we have a route like this
Route::group(''prefix' => 'example', function() {
Route::get('/{key}', 'ExampleController@getKey');
});
But now what if we want to pass a $key variable to getKey() method without explicitly declare it in the route? For example: I want to pass a key that carry the value of foo to the getKey() method
Route::group(['prefix' => 'foo'], function() {
Route::get('/', 'ExampleController@getKey');
})
So the solution must satisfy:
- The method in the second route, must receive
$keyasfoo - The route must start
/foo/, but not anything else likeexample/foo
I don't want the foo value to appear twice in the route, simply it just looks ugly, also I don't want to complicate the existing code in the ExampleController. So what @mrperfectionist did probably solve the problem. I woudn't say this is the only way, I would rather set a default $key value in the getKey() method.
If I face multiple routes that require different $key value to pass to getKey() method, I would consider using the provided lines of code.
return App::call('App\Http\Controllers\DateController@holiday' , ['data' => $data]);
you can pass 2 routes to 1 controller with diffrent URI then check for this uri by
if ($request->routeIs('YourVar.*')) { }
in controller
Please or to participate in this conversation.