Snapey's avatar

You mean instead of using Laravel? Just write your own framework?

mrperfectionist's avatar

:-( :-( :-(

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!

Snapey's avatar

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's avatar

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.

jlrdw's avatar

You are not using a view, is that correct?

bestmomo's avatar

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;
  }
}
1 like
Snapey's avatar

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

jlrdw's avatar

I still think the "from scratch series" is the correct answer.

2 likes
edoc's avatar

is this the answer?

1 like
mrperfectionist's avatar

@michaloravec,

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.

mrperfectionist's avatar
Level 2

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;
}
6 likes
MichalOravec's avatar

It's same as

Route::get('/', function() {
    echo "Name";
});
1 like
Snapey's avatar

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';
2 likes
mrperfectionist's avatar

@snapey,

Okay. But what about the Passing data to Controller? From my solution I can pass parameter as much as I want.

Snapey's avatar

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.

2 likes
automica's avatar

@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.

AngelinCalu's avatar

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

trin's avatar

route.php

Route::group(['name' => 'hello world'], function () {
  Route::get('/', [DateController::class, 'holiday']);
});

DateController.php

holiday ($request) {
	echo $request->route()->action['name'];
}
phamduchuy552's avatar

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:

  1. The method in the second route, must receive $key as foo
  2. The route must start /foo/, but not anything else like example/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]);
Mohamed Magdy's avatar

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.