default value for route parameter in Laravel - php

Hi My website must be multi-lang and everything are OK except one piece for example my homepage route is like below:
Route::get('/{lang}','Welcomecontroller#index');
and in AppServiceProvider in boot method I write function that came in following:
public function boot()
{
$request=Request::capture();
Cookie::queue('lang',$request->lang);
$lang=$request->has('lang') ? $request->lang : "fa";
app()->setLocale($lang);
}
problem of my code is here,my code works fine when pass query string like ?lang=en
but in passing parameter in route like get('/{lang}','WelcomeController#index'); if parameter is not exist
404 page showing and always showing fa lang if parameter pass. Is there any way to this method if {lang} parameter isn't exist by default showing for example en lang and url showing just like http://localhost:8000/ against http://localhost:8000/en and how get {lang} parameter from url in that method.

You can make it nullable like that
Route::get('/{lang?}','Welcomecontroller#index');
And inside your controller Welcomecontroller in index function you can pass default value like that:
public function index($lang = 'fa'){
// code
}
you can get more information from https://laravel.com/docs/6.x/routing#parameters-optional-parameters

you can use URL::defaults.
also you can get your answer from https://laravel.com/docs/5.6/urls#default-values

Related

"Undefined type 'App'" in api.php Laravel

I'm using Laravel 8.53 and Vuejs.
I want to set language for specific controller based on api parameter. (to send password reset email in desired language)
I have this route in api.php which works:
Route::post('forgot-password', [NewPasswordController::class, 'forgotPassword'])->name('password.reset');
Now I wanted to create something like this:
Route::post('forgot-password/{locale}', function ($locale) {App::setLocale($locale);}, [NewPasswordController::class, 'forgotPassword'])->name('password.reset');
Now when I send to /api/forgot-password/en I get 200 OK but no response.
My VS Code is showing me this error: Undefined type 'App'.
Do I need to define "App" in api.php? How?
I am not sure if it's correlated but maybe the problem is in different place. You passing three parameters to post method. According to laravel docs you should pass only two. In this case you pass callback, or you pass controller path with method. Try to move App::setLocale() to your controller method and use your first syntax. Remember to import App facade before using it.
// api.php
Route::post('forgot-password', [NewPasswordController::class, 'forgotPassword'])->name('password.reset');
// NewPasswordController.php
use \App;
class NewPasswordController {
public function forgotPassword( $locale ) {
App::setLocale( $locale );
/* rest of code */
}
}

How to route URI with a specific Method in CodeIgniter

I have been searching for the correct answer from the last 2hrs but the already answered solutions are not working,
I have a Controller named courses and a method detail I want to redirect only this method to specific Url
My code is :
$route['courses/detail/(:any)'] = 'courses/detail/$1';
if I remove detail it works fine but all methods inside this controller are getting redirected to the same Url.
Please give a solution.
using codeigniter routing method, you can write following code for domain/courses/detail/123 url, it'll pass the language as a parameter to the function. suppose you have index.php controller and about as a function
function detail($course_id)
{
echo $course_id;
}
then you've to write following line in routes.php
$route['courses/detail/(:any)'] = 'courses/detail/$1';
now if you hit domain/courses/detail/123, then output will be 123

Codeigniter: call a controller from another codeigniter file

I have this two file path:
1: /api/math/application/controllers/test.php
2: /learn/mathtek/application/controllers/Auth.php
I need to call the controller from path2(Auth) inside the controller of path1(test). Is this possible? if yes how?
Ive tried to used the redirect() function but didnt work.
Also i tried this:
require_once('/learn/mathtek/application/controllers/Auth.php');
$aObj = new a(); //create object
$aObj->custom_a(); //call function
but it still didnt work...help please ... newbie in codeigniter here
Thanks guys for the help. I ended up doing a direct post of the path using redirect function to make it work.
1: /api/math/application/controllers/test.php - codeigniter1
2: /learn/mathtek/application/controllers/Auth.php - codeigniter2
Examples:
If i want to call path(2) from path(1).
redirect('http://localhost/learn/mathtek/auth/signin'); --'signin' is a function inside 'auth' controller
thats the code inside the 'test' controller. This works in codeigniter even if its from different path of controller.
However this doesnt work for UNITY(WEBGL). Thats why i ended doing an echo and returning it back.
You can't call any other controller action/method for have output or return value in your controller action/method because it is out of rules of MVC.
But you can redirect from one controller action to another controller action and pass argument in another controller action like below:
redirect("CONTROLLER/ACTION/ARGUMENT1/ARGUMENT2");
Edit:
suppose you are in Test controller and in test_method() action of Test controller then you can put your business logic code in the method and got some output and now you want to call any other controller function(eg: Auth) for perform any other operation with that output then you can pass that output in a redirect function as below:
redirect("Auth/auth_method/ARGUMENT1/ARGUMENT2");

What is required to call a controller in Codeigniter?

I'm creating a simple blog with Codeigniter. But I'm having trouble calling another controller besides the default controller.
The following URL takes me to the default controller as specified in my config/routes.php.
blog/index.php
According to the documentation, simply appending the name of another controller saved in controllers/ is all that is needed:
blog/index.php/blog_login
Here is my controller class, named blog_login.php:
class Blog_login extends CI_Controller {
public function index()
{
echo 'It works!';
}
}
But doing this throws a 404 error, which makes me feel that I'm missing something. Is there something else that I am supposed to configure before trying to access a different controller?
http://codeigniter.com/user_guide/general/routing.html Read this properly, it couldn't be clearer.
According to the documentation, simply appending the name of another
controller saved in controllers/ is all that is needed
This is not true. If you want to call another controller 'Blog_login', you simply put the name of the controller as the first segment of the url:
domain.com/index.php/blog_login
This will never work:
blog/index.php/blog_login
Index.php (unless you remove it via .htaccess) always follows right after your domain.com
Finally, you don't need to specify routes unless you're doing something non standard. So
domain.com/index.php/blog_login - calls the index() function in your Blog_login controller
domain.com/index.php/blog - calls the index() function in your blog controller
domain.com/index.php/blog/search - calls the search() function in your blog controller.
None of the above examples need an entry in routes.php
When u call:
blog/index.php/blog_login
you're really calling a method called "blog_login" in your "blog" controller. If you want to call another controller, it must have the following structure:
controller_name/controller_method
So, if you wanna call your blog_login controller just call it like this:
blog_login/
Note: Sometimes it's necessary to add the base_url() to your URL in order to make CI understand correctly the URL.

CodeIgniter: Page not found when passing parameters to a controller?

I'm trying to pass parameters to a control in codeigniter, but I'm getting 404 page not found error, I don't get it, I did what the guide says: http://codeigniter.com/user_guide/general/controllers.html#passinguri
When I remove the params in the index function and just access the controller everything works fine, but I can't pass a value to it...
Here is the code the way I'm trying to send a param:
http://mysite/123
<?php
class Main extends Controller {
function index($username) {
echo $username;
}
}
?>
How can I get more info regarding this error from codeigniter?
Thank you.
With that URL, CodeIgniter can't understand if you want to pass 123 to the index method or if you're requesting the 123 method with no parameters. You have to explicitly name the default method if you need to pass it some parameters.
http://mysite/index/123
Option 1 - Rempap the function call in your controller
If your controller contains a function named _remap(), it will always get called regardless of what your URI contains. It overrides the normal behavior in which the URI determines which function is called, allowing you to define your own function routing rules.
http://codeigniter.com/user_guide/general/controllers.html#remapping
Option 2 - Use a custom route.
http://codeigniter.com/user_guide/general/routing.html

Categories